📂django
🔖跳转
2022-09-15 20:02阅读: 31评论: 0推荐: 0

跳转携带条件

前端按钮

{% edit_btn request 'user_edit' pk=user.id %}

后端逻辑

@register.simple_tag
def edit_btn(request, url_name, *args, **kwargs):
    if not check_per(request, url_name):
        return ''
    # 获取url后的参数
    params = request.GET.urlencode()
    params_string = ''
    if params:
	#实例化QueryDict对象,并让他可修改
        params_dict = QueryDict(mutable=True)
	#fiter=page=5
        params_dict['_fiter'] = params
		与?拼接并encode
        params_string = '?' + params_dict.urlencode()
    url = reverse(url_name, args=args, kwargs=kwargs)
    html_btn = f"""<a href="{url}{params_string}" class="btn btn-warning btn-sm">编辑</a>"""
    return mark_safe(html_btn)

跳转地址处理方法

from django.urls import reverse
def fiter_url(request, url):
    """
    url为url_name
    """
    _fiter = request.GET.get('_fiter')
    if _fiter:
        _fiter = '?' + _fiter
    else:
        _fiter = ''
    url = reverse(url) + f'{_fiter}'
    return url

视图层

def user_edit(request, pk):
    user = User.objects.filter(pk=pk).first()
    if request.method == 'GET':
        form = UserEditForm(instance=user)
        return render(request, 'form2.html', {'form': form})
    if request.method == 'POST':
        form = UserEditForm(data=request.POST, instance=user)
        if not form.is_valid():
            return render(request, 'form2.html', {'form': form})
        form.save()
        return redirect(fiter_url(request, 'user_list'))

本文作者:Sherwin

本文链接:https://www.cnblogs.com/sherwin1995/p/16697818.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Sherwin_szw  阅读(31)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.