Reputation: 107
I try to write the comment on page and then instead redirect on page I have the empty window in browser and
[b'<h1>Not Found</h1><p>The requested URL /post/add_comment/test_single_post/ was not found on this server.</p>']
and AssertionError: 404 != 302
in terminal log.
I can't understand why page is not found (404) in this case.
view
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))
urls
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"),
)
template
<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>
Can anyone give me an answer how to solve this problem?
Upvotes: 1
Views: 1816
Reputation: 14369
You have two regular expressions which are overlapping:
url(r'^post/(?P<slug>\S+)/$', SinglePost.as_view(),
name='single_post'),
[...]
url(r'^post/add_comment/(?P<slug>\S+)/$',
You should change the first one to be less greedy, for example: r'^post/(?P<slug>[^/]+)/$'
or place it at the end.
Upvotes: 0
Reputation: 45575
Your single_post
regex catches all urls started with 'post/'. Place this url at the end of the patterns:
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'),
)
Or, as a more correct solution, change the \S+
regex to valid slug regex [\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"),
)
Upvotes: 2