zoukankan      html  css  js  c++  java
  • 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 %}

    URL路由

    from django.conf.urls import url
    from . import views, list_views
    from haystack.views import SearchView
    
    
    urlpatterns=[
        url(r'^article-column/$', views.article_column, name='article_column'),
        url(r'^rename-column/$', views.rename_article_column, name='rename_article_column'),
        url(r'^delete-column/$', views.delete_article_column, name='delete_column'),
        url(r'^post-article/$', views.article_post, name='article_post'),
        url(r'^article-list/$', views.article_list, name='article_list'),
        url(r'^delete-article/$', views.delete_article, name='delete_article'),
        url(r'^article-detail/(?P<id>d+)/(?P<slug>[-w]+)/$', views.article_detail, name='article_detail'),
        url(r'^re-edit-article/(?P<article_id>d+)$', views.re_edit_article, name='re_edit_article'),
        url(r'^list-article-titles/$', list_views.article_titles, name='list_article_titles'),
        url(r'^list-article-titles-bysomeone/(?P<author>[-w]+)/$', list_views.article_titles_by_someone, name='list_article_titles_bysomeone'),
    
        url(r'^upload/$', views.upload_img, name='upload_img'),
        # url(r'^search/$', views.MySeachView, name='haystack_search'),
        # SearchView()视图函数,默认使用的HTML模板路径为templates/search/search.html
        url(r'search/$', SearchView(), name='haystack_search'),
    ]

    评论视图函数: 

    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)

    在评论视图中如果采用硬编码方式则为

    article_detail_url = 'article/article-detail/%s/%s' % (article_id,slug)
    return redirect(article_detail_url)

    这样做的坏处就是,如果当文章详情页面的url变化之后,这里也要跟着修改,不符合DRY原则(Don`t Repeat Yourself)

    使用reverse函数(意思是逆向、反转)代码中只要写对应url的name就实现了从name到path的转换,那为什么叫反转呢,因为URL配置中实现的是从path到name。

  • 相关阅读:
    cocos2d-x3.6 连连看连通画线
    POJ输出状态的逻辑。
    BeanUtils数据封装与表单JavaBean
    二分法查找 --JS 实现
    Unix/Linux环境C编程新手教程(5) Red Hat Enterprise Linux(RHEL)环境搭建
    我们都傻不啦叽为《围住神经猫》免费推广!
    updating error reports database解决方案
    nutwk的maven中央仓库及配置
    Maven error in eclipse (pom.xml) : Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4
    SFTP环境搭建及客户代码调用公共方法封装
  • 原文地址:https://www.cnblogs.com/gcgc/p/11016330.html
Copyright © 2011-2022 走看看