简体   繁体   English

"将变量 urlname 传递给 django 模板中的 url 标签"

[英]Passing variable urlname to url tag in django template

What I'd like to do (for a recent changes 'widget' - not a django widget in this case) is pass a urlname into my template as a variable, then use it like so: {% url sitechangeobject.urlname %} Where urlname is a string containing a valid name for a url.我想做的(对于最近的更改“小部件” - 在这种情况下不是 django 小部件)是将 urlname 作为变量传递到我的模板中,然后像这样使用它: {% url sitechangeobject.urlname %} Where urlname 是一个字符串,其中包含 url 的有效名称。

Is this possible?这可能吗? The template keeps breaking saying it can't find sitechangeobject.urlname as a name (which is quite right, it doesn't exist).该模板一直在说它找不到 sitechangeobject.urlname 作为名称(这是完全正确的,它不存在)。 Is there any way to make it look inside that variable?有没有办法让它看起来在那个变量里面?

There are other ways to solve this problem if not, just thought I'd check though.如果没有,还有其他方法可以解决这个问题,只是想我会检查一下。

Thanks!谢谢!

As of Django 1.3 the {% url %} tag properly supports: 从Django 1.3开始, {% url %}标记正确支持:

{% url view_name_variable %}
{% url 'view_name_string' %}

...this becomes the default behaviour in Django 1.5. ...这成为Django 1.5中的默认行为。

Previously, you had only the option to do this: 以前,您只能选择执行此操作:

{% url view_name_string %}

To get the tag to work in this way in Django 1.3 and 1.4 projects, you will need the following line to the top of every template you use it in: 要在Django 1.3和1.4项目中以这种方式使标记工作,您需要在您使用它的每个模板的顶部使用以下行:

{% load url from future %}

According to the Django 1.3 release notes : 根据Django 1.3发行说明

...in Django 1.5, the old behavior will be replaced with the new behavior. ...在Django 1.5中,旧行为将被新行为所取代。 To ensure compatibility with future versions of Django, existing templates should be modified to use the new future libraries and syntax. 为确保与Django的未来版本兼容,应修改现有模板以使用新的未来库和语法。

Note that support for {% load url from future %} has been removed in Django 1.9. 请注意,在Django 1.9中删除了对{% load url from future %}支持。

Note: this answer is only really relevant to versions of django before 1.3. 注意:这个答案只与1.3之前的django版本有关。 If you are using django 1.3 or later, the required functionality is built-in - please see meshy's answer . 如果您使用的是django 1.3或更高版本,则内置所需的功能 - 请参阅meshy的答案

The built-in url tag cannot do this. 内置的url标记无法执行此操作。 However django-reversetag does exactly this (and more). 然而, django-reversetag确实如此(以及更多)。

According to the readme, the reverse tag provided by this code provides: 根据自述文件,此代码提供的reverse标记提供:

  • Consistent syntax ("string literals" and variables) 一致的语法(“字符串文字”和变量)
  • Ability to reverse view names stored in context variables 能够反转存储在上下文变量中的视图名称
  • Partial reversing 部分逆转

for django 1.5 may be this is useful 对于django 1.5可能这是有用的

usually, to access a variable passed from view we use {{variable}} 通常,要访问从视图传递的变量,我们使用{{variable}}

however, for url in template, the following does not work: {% url 'app:namespace' {{varible}} %} 但是,对于模板中的网址,以下内容不起作用:{%url'app:namespace'{{varible}}%}

simply use the following is fine: {% url 'app:namespace' varible %} 只需使用以下就可以了:{%url'app:namespace'varible%}

if you are using Django 1.5 and up, django-reversetags is not required anymore for just passing view names as variables into templates, to be used within the url tag. 如果您使用的是Django 1.5及更高版本,则不再需要django-reversetags将视图名称作为变量传递到模板中,以便在url标记中使用。

I was confused with the availability of django-reversetags, just thought of updating the matter correctly here. 我对django-reversetags的可用性感到困惑,只想到这里正确更新问题。

In template ---->在模板中---->
{% url 'app_name:urlName' arg1=value arg2=value %}<\/code>

In app url ---->在应用程序网址中---->

url(r'^goto/(?P<arg1>[0-9A-Za-z_\-]+)/(?P<arg2>[0-9A-Za-z]{1,13}])/', ViewName, name='urlName'),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM