python - 如何将变量传递给 django 中的 {% url %} 标签?

标签 python django templates url

我有 2 个 View :a 和 b 它们都渲染到一个模板 T。 还有 a_2 和 b_2 View 。 T 模板应该生成 a_2 和 b_2 View 的 url。

但是我不能使用 {% url %} 因为我不能将 url 名称作为参数传递。

我无法在 View 中生成整个 url,因为我应该多次生成(一个在表中的一行 - 所以它可能是数百个链接)

我不想写2个一模一样的模板。所以问题是 - 如何避免这种情况?

最佳答案

这在 django 1.5 中已修复。

1.5之前的版本需要使用

{% extends 'base.html' %}

{% load url from future %}

{% url 'name_as_a_string' %}
{% url name_stored_in_variable %}

来自 https://docs.djangoproject.com/en/dev/releases/1.5/

One deprecated feature worth noting is the shift to “new-style” url tag. Prior to Django 1.3, syntax like {% url myview %} was interpreted incorrectly (Django considered "myview" to be a literal name of a view, not a template variable named myview). Django 1.3 and above introduced the {% load url from future %} syntax to bring in the corrected behavior where myview was seen as a variable.

关于python - 如何将变量传递给 django 中的 {% url %} 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14045221/

相关文章:

c++ - 模板继承自模板,但编译器提示未从基本模板声明标识符

c# - 在 C++/CLI 中包装非托管 C++ 模板以供 C#/.NET 使用

python - pytz:仅从 GMT 偏移返回 Olson 时区名称

python - 如何更有效地修改 pandas 数据框列

python - DRF : router for ViewSet with a foreign-key lookup_field

python - 在 html 页面中填写注册表单后,出现此错误并使用用户名和密码并将值存储在登录表单中

c++ - 在 C++ 中选择重载模板函数时的优先级

python - OpenCV 中的 Lucas-Kanade 光流对我不起作用

python - 无法在 Pepper 上的 Choregraphe 中导入 python 模块

Django:在模型管理器中获取模型的表名?