NoReverseMatch at /-找不到带有参数'('',)'的'new_entry'的反向

我已经尝试过上下寻找解决方案,并且花了很多令人沮丧的时间盯着代码,试图找出解决方法。

我是Python的新手,我正在完成Python速成课程,使用Django创建Web应用程序,特别是第19-1章,我必须在其中重新创建制作博客的步骤。

我以为我已经修改了代码以适合该项目,但是随着我不断收到此NoReverseMatch错误,它看起来就不一样了。现在,我了解到它实际上是由于无法通过模板或由于代码错误而无法找到页面而导致的。问题是我似乎看不出有什么问题。

错误是:

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

Error during template rendering
In template C:\Users\mcgar\Documents\blog\blog\templates\blog\base.html, error at line 0

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

1   <p>
2       <a href="{% url 'blog:index' %}">Blog</a>
3   </p>
4   
5   {% block content %}{% endblock content %}

这是我的文件:

views.py

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse

from .models import BlogPost
from .forms import PostForm, EntryForm


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

def new_post(request):
    """Add a new title"""
    if request.method != 'POST':
        """No data submitted, blank form"""
        form = PostForm()
    else:
        #POST submitted data
        form = PostForm(data=request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('blog:index'))

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

def new_entry(request, post_id):
    """Add new entry for a post"""
    post = BlogPost.objects.get(id=post_id)

    if request.method != 'POST':
        #No data submitted
        form = EntryForm()
    else:
        #POST data submitted
        form = EntryForm(data=request.POST)
        if form.is_valid():
            new_entry = form.save(commit=False)
            new_entry.post = post
            new_entry.save()
            return HttpResponseRedirect(reverse('blog:index', post_id=post_id))

    #Display blank or invalid form
    context = {'text': post, 'form': form}
    return render(request, 'blog/new_entry.html', context)

base.html(基本模板)

<p>
    <a href="{% url 'blog:index' %}">Blog</a>
</p>

{% block content %}{% endblock content %}

index.html

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

{% block content %}

<p>Blog Posts</p>

<ul>
  {% for post in posts %}
   <li>{{ post }}</li>
  {% empty %}
   <li>No posts have been added yet.</li>

  {% endfor %}

    <a href="{% url 'blog:new_post' %}">Add a new post</a>
      <p>
        <a href="{% url 'blog:new_entry' post.id %}">Add a new entry</a>
    </p>
</ul>

{% endblock content %}

new_post.html

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

{% block content %}

<p>New post</p>
<p>Title:</p>

<form action="{% url 'blog:new_post' %}" method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button name="submit">Add title</button>
</form>

{% endblock content %}

new_entry.html

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

{% block content %}

<p><a href="{% url 'blog:index' post.id %}">{{ post }}</a></p>

<p>Add entry:</p>
<form action="{% url 'blog:new_entry' post.id %}" method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button name="submit">Add entry</button>
</form>

{% endblock content %}

urls.py

"""Defines URL patterns for blog"""

from django.urls import path, include
from . import views

app_name = 'blog'
urlpatterns = [
    #home page
    path('', views.index, name='index'),
    #page for new posts
    path('new_post/', views.new_post, name='new_post'),
    #Adding a new entry
    path('new_post/<int:post_id>/', views.new_entry, name="new_entry"),

]

models.py

from django.db import models

# Create your models here.
class BlogPost(models.Model):
    '''A blog'''
    title = models.CharField(max_length=200)
    text = models.TextField()
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        '''Return a string representation of the model.'''
        return self.title

表格

from django import forms
from .models import BlogPost

class PostForm(forms.ModelForm):
    class Meta:
        model = BlogPost
        fields = ['title']
        labels = {'title': ''}

class EntryForm(forms.ModelForm):
    class Meta:
        model = BlogPost
        fields = ['text']
        labels = {'text': ''}
        widgets = {'text': forms.Textarea(attrs={'cols':80})}

对于我的一生,我无法弄清楚,因此任何帮助都会挽救我的理智。

非常感谢你!

评论