demand:
Assuming the article details page review articles need to refresh the display of the page (the original method, the comment form is submitted form way, not using ajax mode),
After submitting the comment code to go comment view functions, such as data warehousing after page needs to be relocated to the article details page.
article_detail2.html page
<!-- article_detail2.html --> <! - comment -> < br > {% if user.is_authenticated %} <div> <form action="{% url 'comment:post_comment' article.id %}" method="POST" > {% csrf_token %} <div class="form-group"> <label for="body"> <strong> I also speak: </strong> </label> <textarea type="text" class="form-control" id="body" name="body" rows="2"></textarea> </div> <!-- 提交按钮 --> <button type="submit" class="btn btn-primary ">发送</button> </form> </div> <br> {% else %} <br> <h5 class="row justify-content-center"> 请<a href="{% url 'account:user_login' %}">登录</a>后回复 </h5> <br> {% endif %}
Comments view function:
from django.http import HttpResponse from django.shortcuts import render, get_object_or_404, redirect, reverse from article.models import ArticlePost from comment.forms import CommentForm from .models import Comment from utils.decorators import login_wrapper # Create your views here. # @login_wrapper def post_comment(request, article_id, parent_comment_id=None): article = get_object_or_404(ArticlePost, id=article_id) slug = article.slug print(article_id, parent_comment_id) # 处理post请求 if request.method == 'POST': comment_form = CommentForm(request.POST) if comment_form.is_valid(): new_comment = comment_form.save(commit=False) new_comment.article = article new_comment.user = request.user # Two Reply IF parent_comment_id: Print ( ' has requested ' ) parent_comment = Comment.objects.get (the above mentioned id = parent_comment_id) # If the reply over two levels, would be transformed into two new_comment.parent_id = parent_comment.get_root (). the above mentioned id # is reply to people new_comment.reply_to = parent_comment.user new_comment.save() Print ( ' comment has been written ' ) return HttpResponse ( ' 200 the OK ' ) new_comment.save() # Comment complete refresh the page, submit data to optimize later aja, partial refresh # return redirect ( 'Article This article was: list_article_titles') # need to pass parameters when reverse reverse url, to use kwargs parameters, will pass a parameter of type dictionary = Reverse article_detail_url ( ' Article This article was: article_detail ' , kwargs = { ' ID ' : the article_id, ' Slug ' : Slug}) return the redirect (article_detail_url) the else : return the HttpResponse ( " form error, re-fill " ) elif request.method == ' GET ' : Print ( ' get comments ' ) comment_form = CommentForm() context = { 'comment_form': comment_form, 'article_id': article_id, 'parent_comment_id': parent_comment_id } return render(request, 'comment/reply.html', context) the else : return the HttpResponse ( " accepts only get or post requests " )
Where you can see these two lines:
article_detail_url = reverse('article:article_detail', kwargs={'id':article_id,'slug':slug}) return redirect(article_detail_url)
The first line