30👍
As of Django 1.3 the {% url %}
tag properly supports:
{% url view_name_variable %}
{% url 'view_name_string' %}
…this becomes the default behaviour in 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:
{% load url from future %}
According to the Django 1.3 release notes:
…in Django 1.5, the old behavior will be replaced with the new behavior. To ensure compatibility with future versions of Django, existing templates should be modified to use the new future libraries and syntax.
Note that support for {% load url from future %}
has been removed in Django 1.9.
8👍
Note: this answer is only really relevant to versions of django before 1.3. If you are using django 1.3 or later, the required functionality is built-in – please see meshy’s answer.
The built-in url
tag cannot do this. However django-reversetag does exactly this (and more).
According to the readme, the reverse
tag provided by this code provides:
- Consistent syntax (“string literals” and variables)
- Ability to reverse view names stored in context variables
- Partial reversing
- [Django]-Django testing: Test the initial value of a form field
- [Django]-Django count RawQuerySet
- [Django]-Django TextField and CharField is stripping spaces and blank lines
4👍
for django 1.5
may be this is useful
usually, to access a variable passed from view
we use
{{variable}}
however, for url in template, the following does not work:
{% url 'app:namespace' {{varible}} %}
simply use the following is fine:
{% url 'app:namespace' varible %}
- [Django]-How do I remove Label text in Django generated form?
- [Django]-How do you dynamically hide form fields in Django?
- [Django]-No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model
1👍
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.
I was confused with the availability of django-reversetags, just thought of updating the matter correctly here.
- [Django]-Unittest Django: Mock external API, what is proper way?
- [Django]-Error: No module named staticfiles
- [Django]-How do I deploy Django on AWS?
0👍
In template —->
{% url 'app_name:urlName' arg1=value arg2=value %}
In app url —->
url(r'^goto/(?P<arg1>[0-9A-Za-z_\-]+)/(?P<arg2>[0-9A-Za-z]{1,13}])/', ViewName, name='urlName'),
In project Url—->
path('v1/to_app/', include(('app_name.urls','app_name'),namespace="app_name")),
This will help…!
- [Django]-Django models avoid duplicates
- [Django]-How to make an auto-filled and auto-incrementing field in django admin
- [Django]-Warning: Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'