Django模板中的url的NoReverseMatch

时间:2018-01-01 15:59:17

标签: python django

我收到此错误:

@isset(...)

由于此模板代码:

NoReverseMatch at /workoutcal/add/2017/11/1/
Reverse for 'add_workout' not found. 'add_workout' is not a valid view function or pattern name.

来自urls.py:

<form action="{% url 'add_workout' date.year date.month date.day %}" method="post">

我跟随docs。不知道为什么我的网址名称找不到。有什么想法吗?

编辑:

整个urls.py

url(r'^add/(?P<year>[0-9]+)/(?P<month>[0-9]+)/(?P<day>[0-9]+)/$', views.AddWorkoutView.as_view(), name = 'add_workout'), 

呈现模板的视图:

from django.conf.urls import url
from . import views

app_name = 'workoutcal'
urlpatterns = [
    url(r'^$', views.redirect_to_calendar),
    url(r'^(?P<year>[0-9]+)/(?P<month>[1-9]|1[0-2])$', views.calendar, name='calendar'),
    url(r'^(?P<id>[0-9]+)/$', views.detail, name='detail'), #Detail of a workout. Shows lifts, duration, etc. All the attributes.
    url(r'^add/(?P<year>[0-9]+)/(?P<month>[0-9]+)/(?P<day>[0-9]+)/$', views.AddWorkoutView.as_view(), name = 'add_workout'),
    url(r'^edit/(?P<id>[0-9]+)/$', views.edit, name='edit'), #Editing an existing workout. id is id of workout object to edit.
    url(r'^name/$', views.get_contact_form, name='contact_form'),
    url(r'^get_lifts/$', views.get_lifts, name='get_lifts'),
    url(r'^add_lift/$', views.add_lift, name='add_lift'),
    url(r'^get_cardio/$', views.get_cardio, name='get_cardio'),
    url(r'^login/$', views.LoginView.as_view(), name='login'),
    url(r'^logout/$', views.logout_view, name='logout'),
    url(r'^register/$', views.UserFormView.as_view(), name='register'),
    url(r'^username/$', views.get_username, name='username'),
]

1 个答案:

答案 0 :(得分:1)

因为您使用命名空间定义了您的网址:

app_name = 'workoutcal'

所以你需要在url reverse中包含名称空间:

<form action="{% url 'workoutcal:add_workout' date.year date.month date.day %}" method="post">