返回“post_draft_list”,并没有找到“()”参数和“{}”关键字参数。0模式(s)尝试:[]

时间:2022-06-17 04:24:14

I'm working on DjangoGirls Django Extensions Tutorial and I'm hitting an error. The web app runs perfect locally, but isn't running on when pulled on pythonanywhere. I don't know what's wrong.

我正在学习DjangoGirls Django扩展教程,我遇到了一个错误。这个web应用程序在本地运行得很好,但在使用pythonanywhere时,它并没有运行。我不知道怎么了。

Here is the error message:

这里是错误信息:

Environment:


Request Method: GET
Request URL: --

Django Version: 1.10
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'blog']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template /home/vin/my-first-blog/blog/templates/blog/post_list.html, error at line 0
   Reverse for 'post_draft_list' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []   1 : {% extends 'blog/base.html' %}
   2 : 
   3 : {% block content %}
   4 :     {% for post in posts %}
   5 :         <div class="post">
   6 :             <div class="date">
   7 :                 {{ post.published_date }}
   8 :             </div>
   9 :             <h1><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
   10 :             <p>{{ post.text|linebreaksbr }}</p>


Traceback:

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/vin/my-first-blog/blog/views.py" in post_list
  11.     return render(request, 'blog/post_list.html', {'posts': posts})

File "/usr/local/lib/python3.5/dist-packages/django/shortcuts.py" in render
  30.     content = loader.render_to_string(template_name, context, request, using=using)

File "/usr/local/lib/python3.5/dist-packages/django/template/loader.py" in render_to_string
  68.     return template.render(context, request)

File "/usr/local/lib/python3.5/dist-packages/django/template/backends/django.py" in render
  66.             return self.template.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render
  208.                     return self._render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in _render
  199.         return self.nodelist.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render
  994.                 bit = node.render_annotated(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render_annotated
  961.             return self.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/loader_tags.py" in render
  174.         return compiled_parent._render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in _render
  199.         return self.nodelist.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render
  994.                 bit = node.render_annotated(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render_annotated
  961.             return self.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/defaulttags.py" in render
  323.                 return nodelist.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render
  994.                 bit = node.render_annotated(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render_annotated
  961.             return self.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/defaulttags.py" in render
  447.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "/usr/local/lib/python3.5/dist-packages/django/urls/base.py" in reverse
  91.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

File "/usr/local/lib/python3.5/dist-packages/django/urls/resolvers.py" in _reverse_with_prefix
  389.             (lookup_view_s, args, kwargs, len(patterns), patterns)

Exception Type: NoReverseMatch at /
Exception Value: Reverse for 'post_draft_list' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

my-first-blog/blog/templates/blog/post_draft_list.html

my-first-blog /博客/模板/博客/ post_draft_list.html

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

{% block content %}
    {% for post in posts %}
        <div class="post">
            <p class="date">created: {{ post.created_date|date:'d-m-Y' }}</p>
                <h1><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
            <p>{{ post.text|truncatechars:200 }}</p>
        </div>
    {% endfor %}
{% endblock %}

my-first-blog/blog/urls.py

my-first-blog /博客/ urls . py

from django.conf.urls.defaults import *
from . import views

urlpatterns = [
    url(r'^$', views.post_list, name='post_list'),
    url(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
    url(r'^post/new/$', views.post_new, name='post_new'),
    url(r'^post/(?P<pk>\d+)/edit/$', views.post_edit, name='post_edit'),
    url(r'^drafts/$', views.post_draft_list, name='post_draft_list'),
    url(r'^post/(?P<pk>\d+)/publish/$', views.post_publish, name='post_publish'),
    url(r'^post/(?P<pk>\d+)/remove/$', views.post_remove, name='post_remove'),
    url(r'^post/(?P<pk>\d+)/comment/$', views.add_comment_to_post, name='add_comment_to_post'),
    url(r'^comment/(?P<pk>\d+)/approve/$', views.comment_approve, name='comment_approve'),
    url(r'^comment/(?P<pk>\d+)/remove/$', views.comment_remove, name='comment_remove'),
]

my-first-blog/blog/views.py

my-first-blog /博客/ views.py

from django.shortcuts import render, get_object_or_404, redirect
from django.utils import timezone
from .models import Post, Comment
from .forms import PostForm, CommentForm
from django.contrib.auth.decorators import login_required
from django.http import HttpResponsePermanentRedirect
from django.core.urlresolvers import reverse

def post_list(request):
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, 'blog/post_list.html', {'posts': posts})

# Create your views here.
def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blog/post_detail.html', {'post': post})

@login_required
def post_new(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.published_date = timezone.now()
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = PostForm()
    return render(request, 'blog/post_edit.html', {'form': form})

@login_required
def post_edit(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.published_date = timezone.now()
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = PostForm(instance=post)
    return render(request, 'blog/post_edit.html', {'form': form})

@login_required
def post_draft_list(request):
    posts = Post.objects.filter(published_date__isnull=True).order_by('created_date')
    return render(request, 'blog/post_draft_list.html', {'posts': posts})

@login_required
def post_publish(request, pk):
    post = get_object_or_404(Post, pk=pk)
    post.publish()
    return redirect('post_detail', pk=pk)

@login_required
def post_remove(request, pk):
    post = get_object_or_404(Post, pk=pk)
    post.delete()
    return redirect('post_list')

def add_comment_to_post(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'blog/add_comment_to_post.html', {'form': form})

@login_required
def comment_approve(request, pk):
    comment = get_object_or_404(Comment, pk=pk)
    comment.approve()
    return redirect('post_detail', pk=comment.post.pk)

@login_required
def comment_remove(request, pk):
    comment = get_object_or_404(Comment, pk=pk)
    comment.delete()
    return redirect('post_detail', pk=comment.post.pk)

my-first-blog/blog/templates/blog/base.html

my-first-blog /博客/模板/博客/ base.html

{% load staticfiles %}
<html>
    <head>
        <title>Vin's blog</title>
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
        <link href='//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" href="{% static 'css/blog.css' %}">
    </head>
<body>
    <div class="page-header">
        {% if user.is_authenticated %}
            <a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
            <a href="{% url 'post_draft_list' %}" class="top-menu"><span class="glyphicon glyphicon-edit"></span></a>
            <p class="top-menu">Hello {{ user.username }} <small>(<a href="{% url 'logout' %}">Log out</a>)</small></p>
        {% else %}
            <a href="{% url 'login' %}" class="top-menu"><span class="glyphicon glyphicon-lock"></span></a>
        {% endif %}
        <h1><a href="/">Vin's Blog</a></h1>
    </div>
    <div class="content container">
        <div class="row">
            <div class="col-md-8">
            {% block content %}
            {% endblock %}
            </div>
        </div>
    </div>
</body>
</html>

my-first-blog/blog/templates/blog/post_list.html

my-first-blog /博客/模板/博客/ post_list.html

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

{% block content %}
    {% for post in posts %}
        <div class="post">
            <div class="date">
                {{ post.published_date }}
            </div>
            <h1><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
            <p>{{ post.text|linebreaksbr }}</p>
            <a href="{% url 'post_detail' pk=post.pk %}">Comments: {{ post.approved_comments.count }}</a>
        </div>
    {% endfor %}
{% endblock content %}

my-first-blog/blog/templates/blog/post_details.html

my-first-blog /博客/模板/博客/ post_details.html

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

{% block content %}
    <div class="post">
{% if post.published_date %}
    <div class="date">
        {{ post.published_date }}
    </div>
{% else %}
    <a class="btn btn-default" href="{% url 'post_publish' pk=post.pk %}">Publish</a>
{% endif %}

{% if user.is_authenticated %}
    <a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}">
        <span class="glyphicon glyphicon-pencil"></span>
    </a>
    <a class="btn btn-default" href="{% url 'post_remove' pk=post.pk %}"><span class="glyphicon glyphicon-remove"></span></a>

{% endif %}        

    <h1>{{ post.title }}</h1>
        <p>{{ post.text|linebreaksbr }}</p>
    </div>
    <hr>
    <a class="btn btn-default" href="{% url 'add_comment_to_post' pk=post.pk %}">Add comment</a>

{% for comment in post.comments.all %}
    {% if user.is_authenticated or comment.approved_comment %}
    <div class="comment">
        <div class="date">
            {{ comment.created_date }}
            {% if not comment.approved_comment %}
                <a class="btn btn-default" href="{% url 'comment_remove' pk=comment.pk %}"><span class="glyphicon glyphicon-remove"></span></a>
                <a class="btn btn-default" href="{% url 'comment_approve' pk=comment.pk %}"><span class="glyphicon glyphicon-ok"></span></a>
            {% endif %}
        </div>
        <strong>{{ comment.author }}</strong>
        <p>{{ comment.text|linebreaks }}</p>
    </div>
    {% endif %}
{% empty %}
    <p>No comments here yet :(</p>
{% endfor %}
{% endblock %}

SOLVED: run: "python manage.py migrate blog" on pythonprogramming not just locally.

解决:运行:“python管理。py迁移博客“关于python编程,而不仅仅是本地的。

2 个解决方案

#1


0  

Change the url in your template to this.

将模板中的url更改为这个。

<h1><a href="{% url 'blog:post_detail' pk=post.pk %}">{{ post.title }}</a></h1>

And changes accordingly in all other places wherever you've used {% url 'name' %}. Perhaps you have used post_draft_list in your base.html. Change there accordingly.

在所有使用了{% url 'name' %}的地方都会发生相应的变化。也许您在base.html中使用了post_draft_list。相应的变化。

Another way, which I prefer more. Wherever, you have a pk in your urls change those accordingly this one.

另一种方式,我更喜欢。无论在哪里,你的url中有一个pk相应的改变这些。

<h1><a href="/post/{{ post.id }}/">{{ post.title }}</a></h1>

For post_draft_list do this.

post_draft_list这样做。

<a href="/drafts/">Draft</a>

#2


-1  

According to your question you have error in

根据你的问题,你有错误。

post_draft_list

URL but in above you did not mentioned anywhere this URL in template.

URL但在上面你没有在模板中提到这个URL。

I think, You are using post_draft_list URL in base.html that cause this error. So add base.html here also to identify your error.

我认为,您使用的是post_draft_list URL。导致这个错误的html。所以添加基础。这里的html也可以识别你的错误。

use {% url 'blog:post_draft_list' %} in base.html file

在base中使用{% url 'blog:post_draft_list' %}。html文件

#1


0  

Change the url in your template to this.

将模板中的url更改为这个。

<h1><a href="{% url 'blog:post_detail' pk=post.pk %}">{{ post.title }}</a></h1>

And changes accordingly in all other places wherever you've used {% url 'name' %}. Perhaps you have used post_draft_list in your base.html. Change there accordingly.

在所有使用了{% url 'name' %}的地方都会发生相应的变化。也许您在base.html中使用了post_draft_list。相应的变化。

Another way, which I prefer more. Wherever, you have a pk in your urls change those accordingly this one.

另一种方式,我更喜欢。无论在哪里,你的url中有一个pk相应的改变这些。

<h1><a href="/post/{{ post.id }}/">{{ post.title }}</a></h1>

For post_draft_list do this.

post_draft_list这样做。

<a href="/drafts/">Draft</a>

#2


-1  

According to your question you have error in

根据你的问题,你有错误。

post_draft_list

URL but in above you did not mentioned anywhere this URL in template.

URL但在上面你没有在模板中提到这个URL。

I think, You are using post_draft_list URL in base.html that cause this error. So add base.html here also to identify your error.

我认为,您使用的是post_draft_list URL。导致这个错误的html。所以添加基础。这里的html也可以识别你的错误。

use {% url 'blog:post_draft_list' %} in base.html file

在base中使用{% url 'blog:post_draft_list' %}。html文件