简体   繁体   English

Django 模板渲染期间出现 NoReverseMatch 错误

[英]Django NoReverseMatch Error during Template Rendering

I am setting up a simple blog site with Django, and run into this error when trying to link to a page that allows users to edit existing blog posts.我正在使用 Django 设置一个简单的博客站点,并在尝试链接到允许用户编辑现有博客文章的页面时遇到此错误。

Reverse for 'edit_post' with arguments '('',)' not found. 1 pattern(s) tried: ['edit_post/(?P<title_id>[0-9]+)/$']

If I understand this error correctly, it means that Django can't find a urlpattern that matches the url being requested.如果我对这个错误的理解是正确的,那就意味着 Django 找不到与所请求的 url 相匹配的 urlpattern。 To my eyes I have the urlpattern set up correctly, but I still get the error.在我看来,我的 urlpattern 设置正确,但我仍然收到错误。

The link in question appears on the text.html template, which is the template that displays the text of a particular blog post.有问题的链接出现在 text.html 模板上,该模板显示特定博客文章的文本。

Here is the relevant code:这是相关代码:

urls.py网址.py

"""Defines URL patterns for blogs."""

from django.urls import path

from . import views

app_name = 'blogs'
urlpatterns = [
    # Home page, shows all posts in chronological order.
    path('', views.index, name='index'),
    # A page to show the text of a specific post.
    path('text/<int:title_id>/', views.text, name='text'),
    # Page for adding a new post.
    path('new_post/', views.new_post, name='new_post'),
    # Page for editing a post.
    path('edit_post/<int:title_id>/', views.edit_post, name='edit_post'),
]

views.py视图.py

from django.shortcuts import render, redirect

from .models import BlogPost 
from .forms import BlogPostForm

def index(request):
    """The home page for blogs, shows all posts."""
    titles = BlogPost.objects.order_by('date_added')
    context = {'titles': titles}
    return render(request, 'blogs/index.html', context)

def text(request, title_id):
    """Show a single post title and its text."""
    title = BlogPost.objects.get(id=title_id)
    text = title.text
    context = {'title': title, 'text': text}
    return render(request, 'blogs/text.html', context)

def new_post(request):
    """Add a new post."""
    if request.method != 'POST':
        # No data submitted; create a new form.
        form = BlogPostForm()
    else:
        # POST data submitted; process data.
        form = BlogPostForm(data=request.POST)
        if form.is_valid():
            new_post = form.save(commit=False)
            new_post.save()
            return redirect('blogs:index')

    # Display a blank or invalid form.
    context = {'form': form}
    return render(request, 'blogs/new_post.html', context)

def edit_post(request, title_id):
    """Edit an existing post."""
    post = BlogPost.objects.get(id=title_id)

    if request.method != 'POST':
        # Initial request: pre-fill form with the current post.
        form = BlogPostForm(instance=post)
    else:
        # Post data submitted; process data.
        form = BlogPostForm(instance=post, data=request.POST)
        if form.is_valid():
            form.save()
            return redirect('blogs:index')

    context = {'post': post, 'form': form}
    return render(request, 'blogs/edit_post.html', context)

index.html (this is the homepage for the blog) index.html(这是博客的主页)

{% extends "blogs/base.html" %}

{% block content %}
<p>Blog is a generic site for a blogger to post content for their audience.</p>

    <p>Posts</p>

    <ul>
        {% for title in titles %}
            <li>
                <a href="{% url 'blogs:text' title.id %}">{{ title }}</a>
            </li>
        {% empty %}
            <li>No posts have been added yet.</li>
        {% endfor %}
    </ul>

    <a href="{% url 'blogs:new_post' %}">Create a new post</a>

{% endblock content %}

text.html (this page displays the text content of a particular post, and also the link to edit the post) text.html(此页面显示特定帖子的文本内容,以及编辑帖子的链接)

{% extends "blogs/base.html" %}

{% block content %}

    <p>Title: {{ title }}</p>

    <p>{{ text }}</p>

    <a href="{% url 'blogs:edit_post' title.id %}">Edit post</a>

{% endblock content %}

edit_post.html (this page should display the existing post and allow it to be edited) edit_post.html(此页面应显示现有帖子并允许对其进行编辑)

{% extends "blogs/base.html" %}

{% block content %}
    
    <p>Edit post:</p>

    <p>Title:</p>

    <form action="{% url 'blogs:edit_post' title.id %}" method='post'>
        {% csrf_token %}
        {{ form.as_p }}
        <button name="submit">Save changes</button>
    </form>

{% endblock content %}

How the edit_post function in views.py should work (in theory) is to create an instance based upon the post's title id, and then allowing the user to edit it and save changes. views.py 中的edit_post function 应该如何工作(理论上)是根据帖子的标题 ID 创建一个实例,然后允许用户编辑它并保存更改。

I'm not sure where I'm going wrong here and any suggestions are greatly appreciated.我不确定我哪里出错了,非常感谢任何建议。

The name of the post object you pass to the template, is not title , but post :您传递给模板的帖子 object 的名称不是title ,而是post

{% extends "blogs/base.html" %}

{% block content %}
    
    <p>Edit post:</p>

    <p>Title:</p>

    <form action="{% url 'blogs:edit_post' post.pk %}" method='post'>
        {% csrf_token %}
        {{ form.as_p }}
        <button name="submit">Save changes</button>
    </form>

{% endblock content %}

If you use title.id , it will not find that variable, and thus this will be resolved to the empty string.如果您使用title.id ,它将找不到该变量,因此这将被解析为空字符串。 If you use post.id , or post.pk , it will resolve to the .id field, or primary key of the post object.如果您使用post.idpost.pk ,它将解析为.id字段或post object 的主键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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