简体   繁体   中英

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')

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM