简体   繁体   中英

Django NoReverseMatch in form redirect

I am getting the following error:

NoReverseMatch at /lld/new/

Reverse for 'display_lld' with arguments '()' and 
keyword arguments '{'slug': u'stack-overflow-new-document'}' not found.
0 pattern(s) tried: []

I can't get to the bottom of it, though I think it has something to do with either my url regex or the document.slug variable passed to the index.html template in views.py .

views.py:

from django.shortcuts import get_object_or_404, render, redirect

from .models import Document
from .forms import DocumentForm


def index(request):
    document_list = Document.objects.order_by('-date_updated')
    context = {'document_list': document_list}
    return render(request, 'lld/index.html', context)


def display_lld(request, slug):
    document = get_object_or_404(Document, slug=slug)
    return render(request, 'lld/display_lld.html', {'document': document})


def new_lld(request):
    if request.method == "POST":
        form = DocumentForm(request.POST)
        if form.is_valid():
            document = form.save(commit=False)
            document.save()
            return redirect('display_lld', slug=document.slug)
    else:
        form = DocumentForm()
    return render(request, 'lld/new_lld.html', {'form': form})

site urls.py:

urlpatterns = [
    url(r'^lld/', include('lld.urls', namespace="lld")),
    url(r'^admin/', include(admin.site.urls)),
]

app urls.py:

from django.conf.urls import url

from . import views

urlpatterns = [
    # example: /lld/
    url(r'^$', views.index, name='index'),
    # example: /lld/new/
    url(r'^new/$', views.new_lld, name='new_lld'),
    # ex: /lld/customername-projectname/
    url(r'^(?P<slug>([\w-]+))/', views.display_lld, name='display_lld'),
]

index.html:

{% if document_list %}
    <ul>
    {% for document in document_list %}
        <li><a href="{% url 'lld:display_lld' document.slug %}">{{ document.customer }} / {{ document.title }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No documents are available.</p>
{% endif %}
<a href="{% url 'lld:new_lld' %}">Create New LLD</a>

The form creates the new document fine, it shows up in the admin. But when I click on the forms save button it brings up the NoReverseMatch error rather than redirecting back to the created document. The newly created document is listed on the index page and I can navigate to it by clicking on it's link there, it just appears to throw the error in the form redirect.

When calling redirect , you have left out the lld namespace. You need to include the namespace when you use redirect or reverse , the same way as you already do when you use the {% url %} tag in your templates:

return redirect('lld:display_lld', slug=document.slug)
from django.core.urlresolvers import reverse

return redirect(reverse('lld:display_lld', args=[document.slug]))

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