在此服务器上找不到请求的 URL。姜戈
The requested URL was not found on this server. Django
我尝试在页面上写评论然后在页面上重定向我在浏览器中有空 window 和
[b'<h1>Not Found</h1><p>The requested URL /post/add_comment/test_single_post/ was not found on this server.</p>']
和 AssertionError: 404 != 302
在终端日志中。
我不明白为什么在这种情况下找不到页面 (404)。
查看
class SinglePost(DetailView):
model = Post
template_name = 'post.html'
def get_context_data(self, **kwargs):
comment_form = CommentForm
context = super(SinglePost, self).get_context_data(**kwargs)
comments = Comments.objects.filter(comment_id=self.object).order_by('-added')
context['comments'] = comments
context['form'] = comment_form
return context
@csrf_protect
def add_comment(request, slug):
"""
Add comment to.
"""
if request.POST:
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.comment = Post.objects.get(slug=slug)
form.save()
return redirect('/post/{0}/'.format(slug))
网址
urlpatterns = patterns('',
url(r'^post/(?P<slug>\S+)/$', SinglePost.as_view(),
name='single_post'),
url(r'^tag/(?P<slug>\S+)/$', TagView.as_view(),
name='tagger'),
url(r'^post/add_comment/(?P<slug>\S+)/$',
'blog.views.add_comment', name="commenter"),
url(r'^$', PostsList.as_view(), name="all_posts"),
)
模板
<h3>Comments:</h3>
{% for comment in comments %}
<p>{{ comment.added }} | {{ comment.author }}</p>
<p>{{ comment.comment_text }}</p>
{% empty %}
<p>There are no comments here yet. Be first :)</p>
{% endfor %}
<form action="/post/add_comment/{{ object.slug }}/" method="POST">
{% csrf_token %}
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.subject.errors }}
<label for="id_author">Add your name:</label><br>
{{ form.author|addclass:"form-control" }}
<br/>
<label for="id_comment_text">Add your comment here:</label><br>
{{ form.comment_text|addclass:'form-control comment-textarea' }}
</div>
<br>
<input type="submit" value="Add comment" class="btn btn-primary">
</form>
谁能告诉我如何解决这个问题?
您的 single_post
正则表达式捕获所有以 'post/' 开头的 url。将这个 url 放在模式的末尾:
urlpatterns = patterns('',
url(r'^tag/(?P<slug>\S+)/$', TagView.as_view(),
name='tagger'),
url(r'^post/add_comment/(?P<slug>\S+)/$',
'blog.views.add_comment', name="commenter"),
url(r'^$', PostsList.as_view(), name="all_posts"),
url(r'^post/(?P<slug>\S+)/$', SinglePost.as_view(),
name='single_post'),
)
或者,作为更正确的解决方案,将 \S+
正则表达式更改为有效的 slug 正则表达式 [\w-]+
:
urlpatterns = patterns('',
url(r'^post/(?P<slug>[\w-]+)/$', SinglePost.as_view(),
name='single_post'),
url(r'^tag/(?P<slug>[\w-]+)/$', TagView.as_view(),
name='tagger'),
url(r'^post/add_comment/(?P<slug>[\w-]+)/$',
'blog.views.add_comment', name="commenter"),
url(r'^$', PostsList.as_view(), name="all_posts"),
)
您有两个重叠的正则表达式:
url(r'^post/(?P<slug>\S+)/$', SinglePost.as_view(),
name='single_post'),
[...]
url(r'^post/add_comment/(?P<slug>\S+)/$',
你应该把第一个改得不那么贪心,例如:r'^post/(?P<slug>[^/]+)/$'
或者放在最后。
我尝试在页面上写评论然后在页面上重定向我在浏览器中有空 window 和
[b'<h1>Not Found</h1><p>The requested URL /post/add_comment/test_single_post/ was not found on this server.</p>']
和 AssertionError: 404 != 302
在终端日志中。
我不明白为什么在这种情况下找不到页面 (404)。
查看
class SinglePost(DetailView):
model = Post
template_name = 'post.html'
def get_context_data(self, **kwargs):
comment_form = CommentForm
context = super(SinglePost, self).get_context_data(**kwargs)
comments = Comments.objects.filter(comment_id=self.object).order_by('-added')
context['comments'] = comments
context['form'] = comment_form
return context
@csrf_protect
def add_comment(request, slug):
"""
Add comment to.
"""
if request.POST:
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.comment = Post.objects.get(slug=slug)
form.save()
return redirect('/post/{0}/'.format(slug))
网址
urlpatterns = patterns('',
url(r'^post/(?P<slug>\S+)/$', SinglePost.as_view(),
name='single_post'),
url(r'^tag/(?P<slug>\S+)/$', TagView.as_view(),
name='tagger'),
url(r'^post/add_comment/(?P<slug>\S+)/$',
'blog.views.add_comment', name="commenter"),
url(r'^$', PostsList.as_view(), name="all_posts"),
)
模板
<h3>Comments:</h3>
{% for comment in comments %}
<p>{{ comment.added }} | {{ comment.author }}</p>
<p>{{ comment.comment_text }}</p>
{% empty %}
<p>There are no comments here yet. Be first :)</p>
{% endfor %}
<form action="/post/add_comment/{{ object.slug }}/" method="POST">
{% csrf_token %}
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.subject.errors }}
<label for="id_author">Add your name:</label><br>
{{ form.author|addclass:"form-control" }}
<br/>
<label for="id_comment_text">Add your comment here:</label><br>
{{ form.comment_text|addclass:'form-control comment-textarea' }}
</div>
<br>
<input type="submit" value="Add comment" class="btn btn-primary">
</form>
谁能告诉我如何解决这个问题?
您的 single_post
正则表达式捕获所有以 'post/' 开头的 url。将这个 url 放在模式的末尾:
urlpatterns = patterns('',
url(r'^tag/(?P<slug>\S+)/$', TagView.as_view(),
name='tagger'),
url(r'^post/add_comment/(?P<slug>\S+)/$',
'blog.views.add_comment', name="commenter"),
url(r'^$', PostsList.as_view(), name="all_posts"),
url(r'^post/(?P<slug>\S+)/$', SinglePost.as_view(),
name='single_post'),
)
或者,作为更正确的解决方案,将 \S+
正则表达式更改为有效的 slug 正则表达式 [\w-]+
:
urlpatterns = patterns('',
url(r'^post/(?P<slug>[\w-]+)/$', SinglePost.as_view(),
name='single_post'),
url(r'^tag/(?P<slug>[\w-]+)/$', TagView.as_view(),
name='tagger'),
url(r'^post/add_comment/(?P<slug>[\w-]+)/$',
'blog.views.add_comment', name="commenter"),
url(r'^$', PostsList.as_view(), name="all_posts"),
)
您有两个重叠的正则表达式:
url(r'^post/(?P<slug>\S+)/$', SinglePost.as_view(),
name='single_post'),
[...]
url(r'^post/add_comment/(?P<slug>\S+)/$',
你应该把第一个改得不那么贪心,例如:r'^post/(?P<slug>[^/]+)/$'
或者放在最后。