Answer the question
In order to leave comments, you need to log in
Why do some django videos check for a non-passing variable in the template?
I'm still tinkering with Django for fun, and I've run into a strange thing.
In some manuals, something like the following code works:
views.py
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug = post, status='published', publish__year = year,
publish__month=month, publish__day=day)
comments = post.comments.filter(active = True)
if request.method == 'POST':
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
new_comment = comment_form.save(commit=False)
new_comment.post = post
new_comment.save()
else:
comment_form = CommentForm()
return render(request, 'blog/post/detail.html', {'post': post, 'comments': comments, 'comment_form': comment_form})
{% if new_comment %}
<h2>Your comment has been added.</h2>
{% else %}
Answer the question
In order to leave comments, you need to log in
As far as I know the code is like this:
{% if new_comment %}
<h2>Your comment has been added.</h2>
{% else %}
Not sure but worth a try
return render(request, 'blog/post/detail.html', locals())
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question