我有 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/