scriptdiddy
scriptdiddy

Reputation: 1265

NoReverseMatch for Django form

I am new to Django and I am running into the NoReverseMatch error. Does anyone know how I can resolve this?

Exception value: Reverse for 'profile_list.html' with arguments '()' and keyword arguments '{}' not found.

edit_profile.html

<h1>Add Profile</h1>

<form action="{% url 'questions-new' %}" method="POST">
  {% csrf_token %}
  <ul>
    {{ form.as_ul }}
  </ul>
  <input type="submit" value="Save" />
</form>

<a href="{% url 'profile-list' %}">back to list</a>

urls.py

from django.conf.urls import patterns, include, url

import questions.views

urlpatterns = patterns('',
    url(r'^$', questions.views.ListProfileView.as_view(),
        name='profile-list'),
    url(r'^new$', questions.views.CreateProfileView.as_view(),
        name='questions-new',),
)

views.py

from django.views.generic import ListView

from questions.models import Profile

from django.core.urlresolvers import reverse
from django.views.generic import CreateView

class ListProfileView(ListView):
    model = Profile
    template_name = 'profile_list.html'

class CreateProfileView(CreateView):

    model = Profile
    template_name = 'edit_profile.html'

    def get_success_url(self):
        return reverse('profile_list.html')

Upvotes: 3

Views: 2338

Answers (2)

alecxe
alecxe

Reputation: 473863

Your reverse call is incorrect. According to the docs:

reverse(viewname[, urlconf=None, args=None, kwargs=None, current_app=None])

viewname is either the function name (either a function reference, or the string version of the name, if you used that form in urlpatterns) or the URL pattern name.

So, replace

reverse('profile_list.html')

with

reverse('profile-list')

profile-list is the URL pattern name, that you've defined in urls.py.

Hope that helps.

Upvotes: 2

Scott Woodall
Scott Woodall

Reputation: 10676

Your get_success_url is wrong. Change it to the following:

def get_success_url(self):
    return reverse('profile-list')

reverse should be used in conjunction with the names that you give inside your urls.py patterns and not template names.

Upvotes: 5

Related Questions