from django.shortcuts import render, get_object_or_404, redirectfrom .models import StaffInfo, Emergencyfrom .forms import StaffInfoForm, EmergencyFormdef staff_info_detail(request, id): staff_info = get_object_or_404(StaffInfo, id=id) emergencies = Emergency.objects.filter(res_id=id) if request.method == 'POST': emergency_form = EmergencyForm(request.POST) if emergency_form.is_valid(): emergency = emergency_form.save(commit=False) emergency.res_id = staff_info emergency.save() return redirect('staff_info_detail', id=id) else: emergency_form = EmergencyForm() return render(request, 'staff_info_detail.html', {'staff_info': staff_info, 'emergencies': emergencies, 'emergency_form': emergency_form})def add_emergency(request, id): staff_info = get_object_or_404(StaffInfo, id=id) if request.method == 'POST': emergency_form = EmergencyForm(request.POST) if emergency_form.is_valid(): emergency = emergency_form.save(commit=False) emergency.res_id = staff_info emergency.save() return redirect('staff_info_detail', id=id) else: emergency_form = EmergencyForm() return render(request, 'add_emergency.html', {'staff_info': staff_info, 'emergency_form': emergency_form})你刚刚写的这段代码,url.py中应该怎么写?
时间: 2024-03-16 20:43:15 浏览: 158
Django框架中render_to_response()函数的使用方法
在 `urls.py` 中,你需要添加以下代码:
```python
from django.urls import path
from . import views
urlpatterns = [
path('staff_info/<int:id>/', views.staff_info_detail, name='staff_info_detail'),
path('add_emergency/<int:id>/', views.add_emergency, name='add_emergency'),
]
```
其中 `<int:id>` 表示这个 URL 需要一个整数参数 `id`,然后将这个参数传递给对应的视图函数。你需要将这些代码添加到你的项目中的 `urls.py` 文件中,并将 `views` 替换为你的视图文件的名称。
阅读全文