加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 12:21:28 所属栏目:Python 来源:网络整理
导读:我有2个视图:a和b它们都渲染到一个模板T. 还有a_2和b_2视图.并且T模板应该为a_2和b_2视图生成网址. 但是我不能使用{%url%}因为我不能将url名称作为参数传递. 我无法在视图中生成整个URL,因为我应该多次执行它(一个在表中的一行 – 所以它可能是链接的hund
我有2个视图:a和b它们都渲染到一个模板T.
还有a_2和b_2视图.并且T模板应该为a_2和b_2视图生成网址.

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

我无法在视图中生成整个URL,因为我应该多次执行它(一个在表中的一行 – 所以它可能是链接的hundreeds)

我不想写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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读