带有查询参数的 Django url 模板 [英] Django url template with query parameters

查看:20
本文介绍了带有查询参数的 Django url 模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过我的视图将查询参数传递到链接中,但它让我无法真正以一种好的方式实现这一目标.

我的模板如下:

<a class="link-button" href="{% url 'videos:index' %}?tag={{ tag }}&page={{ next }}">Next</a>

这将返回我想要的:

http://127.0.0.1:8000/videos/?tag=1&page=2

虽然这可行,但它非常脆弱,无法处理 None 值,因此必须有更好的方法来做到这一点.

我试图通过 urltemplate 标签传递它,但它似乎不是我想要的,因为它需要对路径进行 url 配置更改:

{% url 'videos:index' page=next tag=tag %}

有没有实际的方法可以做到这一点,或者我可以使用模板标签来获取参数?我尝试搜索这个,但它给了我很多旧的结果和更多的路径 url,例如:/videos/page-1/tag-1/ 我不是在寻找.

我希望做这样的事情:

<a href="{% url 'videos:index'}?{% params page=next tag=tag %}">Next</a>

解决方案

没有内置支持,但您可以自己添加.例如,您可以定义以下模板标记.例如,我们可以用粗体构建文件:

app/模板标签/__init__.pyurlparams.py

urlparams.py中,我们定义:

from django 导入模板从 urllib.parse 导入 urlencode注册 = 模板.图书馆()@register.simple_tagdef urlparams(*_, **kwargs):safe_args = {k: v for k, v in kwargs.items() 如果 v 不是 None}如果安全参数:返回 '​​?{}'.format(urlencode(safe_args))返回 '​​'

在模板中,我们可以加载模板标签,然后像这样使用它:

{% load urlparams %}<a href="{% url 'videos:index'}{% urlparams page='1' tag='sometag' %}">下一步</a>

请注意,严格来说,URL 参数可以多次包含 same 键.这在这里不可能.因此,我们无法生成所有可能的 URL 参数,但这通常很少见,而且在我看来,这首先不是一个好主意.

I'm trying to pass query parameters via my view into a link, but it escapes me on how to actually achieve this in a good way.

My template is as following:

<a class="link-button" href="{% url 'videos:index' %}?tag={{ tag }}&page={{ next }}">Next</a>

This returns what I want:

http://127.0.0.1:8000/videos/?tag=1&page=2

While this works, it's quite fragile, does not handle None values and there must be a better way of doing this.

I tried to pass this via the urltemplate tag but it did not seem to be what I was looking for since it requires url config changes for path:

{% url 'videos:index' page=next tag=tag %}

Is there an actual way of doing this or a template tag I can use to get the parameters? I tried searching for this but it gave me a lot of old results and more path urls, like: /videos/page-1/tag-1/ which I'm not looking for.

I was hoping to do something like:

<a href="{% url 'videos:index'}?{% params page=next tag=tag %}">Next</a>

解决方案

There is no builtin support, but you can add one yourself. You can for example define the following template tag. We can for example construct files in boldface:

app/
    templatetags/
        __init__.py
        urlparams.py

Where in urlparams.py, we define:

from django import template
from urllib.parse import urlencode

register = template.Library()

@register.simple_tag
def urlparams(*_, **kwargs):
    safe_args = {k: v for k, v in kwargs.items() if v is not None}
    if safe_args:
        return '?{}'.format(urlencode(safe_args))
    return ''

In the template, we can then load the template tag and then use it like with:

{% load urlparams %}

<a href="{% url 'videos:index'}{% urlparams page='1' tag='sometag' %}">Next</a>

Note that strictly speaking, the URL parameters can contain the same key multiple times. This is here not possible. So we can not generate all possible URL parameters, but this is usually quite rare, and in my opinion not a good idea in the first place.

这篇关于带有查询参数的 Django url 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆