The custom label django (url reverse path decode)

django之自定义标签

django of custom labels

Creating custom labels

  1. Templatetags create a folder in the Applications folder
  2. Creating py script in the folder, such as mytags.py
  3. Processing code is written in custom label mytags.py
#自定义标签传参后,携带原路径参数或跳转路径的获取数据参数
#自定义标签
from django import template
from django.urls import reverse

register = template.Library()

@register.simple_tag
def resolve_url(request,url_name,cid):
    """

    :param request: 请求对象
    :param url_name:  url别名
    :param cid:    客户id
    :return:
    """
    from django.http.request import QueryDict
    custom_query_dict = QueryDict(mutable=True)
    custom_query_dict['next'] = request.get_full_path()   #要跳转回的url
    next_url = custom_query_dict.urlencode()    #将得到的搜索路径url编码

    reverse_url = reverse(url_name,args=(cid,))     #编辑的url    ?next=要跳转的url
    full_path = reverse_url + '?' + next_url
    return full_path

In front page, the data transfer parameters

<a href="{{ resolve_url request "case_edit" foo.id }}"></a>

Back-end view view

from django.shortcuts import render,redirect,HttpResponse,
next_url = request.GET.get("next")
return redirect(next_url)

 

Guess you like

Origin www.cnblogs.com/g15009428458/p/12144847.html