我正在研究DjangoGirls Django Extensions教程,我遇到了错误。 Web应用程序在本地运行完美,但在pythonanywhere上拉时不会运行。我不知道出了什么问题。
以下是错误消息:
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
{% 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
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'),
]
我的先博客/博客/ 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)
我的先博客/博客/模板/博客/ 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>
我的先博客/博客/模板/博客/ 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 %}
我的先博客/博客/模板/博客/ 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 %}
求助:在pythonprogramming上运行:“python manage.py migrate blog”,而不仅仅是本地。
答案 0 :(得分:0)
将模板中的网址更改为此。
<h1><a href="{% url 'blog:post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
并且在您使用{% url 'name' %}
的任何地方都会相应地进行更改。也许您在post_draft_list
中使用了base.html
。相应地改变。
另一种方式,我更喜欢。无论您在网址中有pk
,都会相应地更改这些内容。
<h1><a href="/post/{{ post.id }}/">{{ post.title }}</a></h1>
post_draft_list
执行此操作。
<a href="/drafts/">Draft</a>
答案 1 :(得分:-1)
根据你的问题,你有错误
post_draft_list
网址但在上面你没有提到模板中的这个网址。
我认为,您在base.html中使用导致此错误的post_draft_list
网址。所以在这里添加base.html也可以识别您的错误。
在base.html文件中使用{% url 'blog:post_draft_list' %}