I am working on a simple Django 1.8 application with a template that referencesa url that points to another view. The views are all class-based generic views.
When attempting to load up the page, I get the following error:
NoReverseMatch at /
Reverse for 'add' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'$add/$']
<form action="{% url 'movies:add' %}" method="post">{% csrf_token %}
My app's url.py:
urlpatterns = [
url(r'^$', views.MovieList.as_view(), name="index"),
url(r'^add/$', views.MovieCreate.as_view(), name="add"),
url(r'^(?P<pk>\d+)/delete/$', views.MovieDelete.as_view(), name="delete"),
url(r'^(?P<pk>\d+)/update/$', views.MovieUpdate.as_view(), name="update")
]
The template being used:
<table>
<tr>
<td><a href="?sort=title">Title</a></td>
<td><a href="?sort=year">Year</a></td>
<td><a href="?sort=director">Director</a></td>
</tr>
{% for movie in movie_list %}
<tr>
<td>{{movie.title}}</td>
<td>{{movie.year}}</td>
<td>{{movie.director}}</td>
</tr>
{% endfor %}
</table>
<form action="{% url 'movies:add' %}" method="post">{% csrf_token %}
<input type="submit" value="Add Movie" />
</form>
The two views in question:
class MovieList(ListView):
model = Movie
queryset = Movie.objects.order_by("title", "-year", "director")
context_object_name = "movie_list"
template_name = "movies/index.html"
class MovieCreate(CreateView):
model = Movie
fields = ["title", "year", "director"]
template_name = "movies/add_movie.html"
This is strange to me since the correct URL is being resolved in the template and there are no variables expected by the CreateView
(I think).
This means that the regex used to include your app's urls ends with a
$
. It works for an empty pattern such as yourMovieList
, but not for non-empty patterns. Remove the$
in that regex and it'll work.