Django 通过 url 传递数据,找不到 url 模式中的页面
Django passing data through the url, not finding page in url patterns
我正在尝试通过 url 传递对象的 ID,但即使在它通过其模式尝试路径后也无法找到页面
错误:
Using the URLconf defined in GroomingService.urls, Django tried these URL patterns, in this order:
admin/
[name='Home']
appointment-Maker/
account/
admin-home
view_appointment/<id>/
login/ [name='Login Page']
registration/ [name='Registration Page']
logout [name='Logout Page']
The current path, adminview_appointment/21/, didn’t match any of these.
GroomingService.urls
#urls
urlpatterns = [
path('admin/', admin.site.urls),
path('', Home_view, name="Home"),
path('appointment-Maker/', include('appointmentApp.urls')),
path('account/', include('accountApp.urls')),
path('admin-home', include('adminApp.urls')),
path('view_appointment/<id>/', viewAppointment_view), #this is the page it is not finding
path('login/', Login_view, name='Login Page'),
path('registration/', Registration_view, name='Registration Page'),
path('logout', Logout_view, name='Logout Page')
]
adminApp/views.py viewAppointment_view
def viewAppointment_view(request, id):
appointments = Appointment.objects.get(id = id)
context = {
'appointments' : appointments
}
return render(request, "admin_templates/viewappointment.html", context)
templates/admin_templates viewappointment.html
{% extends 'base.html' %}
{% block content %}
<a>appointment view</a>
{% endblock %}
templates/admin_templates adminhome.html (link 被点击通过此页面)
{% extends 'base.html' %}
{% block content %}
<a>this is the admin page</a>
<br>
{% for a in appointments %}
<a href="adminview_appointment/{{a.id}}/">Client name:{{a.client_dog_name}}</a><br> {% comment %} this is the link that is clicked {% endcomment %}
{% endfor %}
<br>
<a>find month</a>
<form method="POST">
{% csrf_token %}
{{ monthyear }}
<button class="btn btn-primary" type="submit">click</buttom>
</form>
{% endblock %}
如果我遗漏了什么请告诉我,我之前在 adminApp/urls.py 中找到了路径
urlpatterns = [
path('', views.adminhome_view, name="Admin Home"),
]
但将其移动到试图找到 url 的位置。我不知道为什么这可能不起作用。
应该是view_appointment/{{a.id}}
,不是adminview_appointment/{{a.id}}
,但最好用{% url … %}
template tag [Django-doc]。您可以为视图命名:
path('view_appointment/<int:id>/', viewAppointment_view, <strong>name='view_appointment'</strong>),
然后在模板中引用:
<a href="{% url 'view_appointment' id=a.id %}">Client name:{{a.client_dog_name}}</a><br>
我正在尝试通过 url 传递对象的 ID,但即使在它通过其模式尝试路径后也无法找到页面
错误:
Using the URLconf defined in GroomingService.urls, Django tried these URL patterns, in this order:
admin/
[name='Home']
appointment-Maker/
account/
admin-home
view_appointment/<id>/
login/ [name='Login Page']
registration/ [name='Registration Page']
logout [name='Logout Page']
The current path, adminview_appointment/21/, didn’t match any of these.
GroomingService.urls
#urls
urlpatterns = [
path('admin/', admin.site.urls),
path('', Home_view, name="Home"),
path('appointment-Maker/', include('appointmentApp.urls')),
path('account/', include('accountApp.urls')),
path('admin-home', include('adminApp.urls')),
path('view_appointment/<id>/', viewAppointment_view), #this is the page it is not finding
path('login/', Login_view, name='Login Page'),
path('registration/', Registration_view, name='Registration Page'),
path('logout', Logout_view, name='Logout Page')
]
adminApp/views.py viewAppointment_view
def viewAppointment_view(request, id):
appointments = Appointment.objects.get(id = id)
context = {
'appointments' : appointments
}
return render(request, "admin_templates/viewappointment.html", context)
templates/admin_templates viewappointment.html
{% extends 'base.html' %}
{% block content %}
<a>appointment view</a>
{% endblock %}
templates/admin_templates adminhome.html (link 被点击通过此页面)
{% extends 'base.html' %}
{% block content %}
<a>this is the admin page</a>
<br>
{% for a in appointments %}
<a href="adminview_appointment/{{a.id}}/">Client name:{{a.client_dog_name}}</a><br> {% comment %} this is the link that is clicked {% endcomment %}
{% endfor %}
<br>
<a>find month</a>
<form method="POST">
{% csrf_token %}
{{ monthyear }}
<button class="btn btn-primary" type="submit">click</buttom>
</form>
{% endblock %}
如果我遗漏了什么请告诉我,我之前在 adminApp/urls.py 中找到了路径
urlpatterns = [
path('', views.adminhome_view, name="Admin Home"),
]
但将其移动到试图找到 url 的位置。我不知道为什么这可能不起作用。
应该是view_appointment/{{a.id}}
,不是adminview_appointment/{{a.id}}
,但最好用{% url … %}
template tag [Django-doc]。您可以为视图命名:
path('view_appointment/<int:id>/', viewAppointment_view, <strong>name='view_appointment'</strong>),
然后在模板中引用:
<a href="{% url 'view_appointment' id=a.id %}">Client name:{{a.client_dog_name}}</a><br>