Django:reverse反转URL并传递参数

需求:

  假设在文章详情页评论文章后需要重新刷新显示该页面(原始方法,提交评论表单为form方式,未采用ajax方式),

提交评论后代码会走comment的视图函数,等数据入库之后需要将页面重新定位到文章详情页面。

article_detail2.html页面

<!-- article_detail2.html -->

<!-- 发表评论 -->
    <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>
                            我也要发言:
                        </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 %}

评论视图函数:

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

            # 二级回复
            if parent_comment_id:
                print('已请求')
                parent_comment = Comment.objects.get(id=parent_comment_id)
                # 若回复层级超过两级,则转为二级
                new_comment.parent_id = parent_comment.get_root().id
                # 被回复人
                new_comment.reply_to = parent_comment.user
                new_comment.save()
                print('评论已写入')
                return HttpResponse('200 OK')

            new_comment.save()
            # 评论完成刷新该页面,后面优化为aja提交数据,局部刷新
            # return redirect('article:list_article_titles')
            # reverse反转url时需要传参数,要用到kwargs参数,会传入一个字典类型的参数
            article_detail_url = reverse('article:article_detail', kwargs={'id': article_id, 'slug': slug})
            return redirect(article_detail_url)
        else:
            return HttpResponse("表单有误,重新填写")

    elif request.method == 'GET':
        print('获取评论')
        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)

    else:
        return HttpResponse("仅接受get或者post请求")

其中可以看到这两行:

article_detail_url = reverse('article:article_detail', kwargs={'id':article_id,'slug':slug})
return redirect(article_detail_url)

第一行将

猜你喜欢

转载自www.cnblogs.com/gcgc/p/11016330.html
今日推荐