Django template url tag - django

After reading many sources, I am confused about what should be inside {% url "xxx" %} (quotes for Django 1.5). Is it the path to a view function, or is it the name of a named url pattern?
Both seem to be correct, mentioned in different places in official Django documentation.
View function documentation
Named URL pattern documentation

As written in first link, both view function or url name pattern can be used in the {%url%} tag. And the whatever it is put that in quotes.
Warning :
Don’t forget to put quotes around the function path or pattern name!
Changed in Django 1.5: The first parameter used not to be quoted,
which was inconsistent with other template tags. Since Django 1.5, it
is evaluated according to the usual rules: it can be a quoted string
or a variable that will be looked up in the context.

Related

Is it ever possible to delete a URL path : Django

I had in my urls.py file, url patterns for certain data processing requirements and later I realized that from among a bunch of them, one was not intended as its requirement had ceased.
However, when I am trying to delete the particular line (the second pattern in the excerpt of the urls.py reproduced here), I am getting the following error at page load ('holding_list'):
NoReverseMatch at /supp/holding_comp/
Reverse for 'holding_change' not found. 'holding_change' is
not a valid view function or pattern name.
I have checked all the references (I think) wherever the path might have been referred to (including the related template).
urls.py
path('supp/holding_comp/', views.HoldingListView.as_view(), name='holding_list'),
...
...
path('supp/holding_comp/edit/<int:pk>/', views.HoldingUpdateView.as_view(), name='holding_change'),
How do I delete the second url pattern (the one for "edit" view)?
I tried to look for a similar query but failed to (may be for want of a suitable search string). Any clue will be appreciated.
Short answer: there are still things referring to the path, and you should find a way to remove these.
This error means that a template, in this case the template rendered by the 'holding_list' view, contains a {% url ... %} template tag that refers to that path(..).
You thus should search in the template for {% url 'holding_change' ... %} patterns, and decide what to do with links that refer to that path.
Note that this might not be sufficient: in order to be safe, you should search all templates and all Python files for {% url ... %} template tags, redirect(..)s, reverse(..)s, etc. You probably best search for holding_change, and thus look what might still refer to that URL.

Add url parameters to a url in django view

I have a django view that lists several urls on external sites.
When I render them I would like to add a few url parameters to each.
These urls are to an external system and thus not listed in my urls.py. Furthermore, some of the links have a hash '#' so it is not as easy as appending a few parameters to the end of the string.
Based on these requirements it seems the url template tag will not be a good fit. I was wondering if there is a custom filter out that to do this.
You don't need Django's url tag here. The url tag is to resolve to URLs that belong to your application.
However, there is the nice django-spurl library. It allows you to handle query parameters via template tags.
An example from the documentation to add query parameters:
{% spurl base="http://example.com/?foo=bar" add_query="bar=baz" %}
<!--
will result in: http://example.com?foo=bar&bar=baz
-->

Django using mod_wsgi for Sub Urls

Similar questions have been asked before on this site, but I had a doubt as to how my site anchor tags will be replaced when I try to host my website under a suburl.
E.g.
My domain is www.example.com
and my suburl which maps to the Django installation is www.example.com/2010/registration
Now since the anchor tags in my templates (for the links) are of the form of a '/' (to reference the root) succeeded by rest of the url the links are not contained inside www.example.com.
So, for example if my anchor tag is of the form
<a href='/profile'>Profile</a>
Then my anchor tag on the site becomes www.example.com/profile instead of becoming www.example.com/2010/registration/profile/
Is there any possible way to work around this thing ?
Thanks,
Nitin
There are tags which can be used in templates to ensure correct prefix added. Start by reading:
http://docs.djangoproject.com/en/1.2/ref/templates/builtins/#url
As Graham says, use the {% url %} tag in your templates. In views, use the reverse() function, which is equivalent. See the documentation.

View referenced by two urls and url tag

I am using url tag in my template for a view, that is used by two different urls. I am getting the wrong url in one place. Is there any way to force django to retrieve different url? Why it doesn't notify my, that such conflict occured and it doesn't know what to do (since python zen says, that is should refuse temptation to guess).
Code in template:
{% url djangoldap.views.FilterEntriesResponse Entry=entry.path as filter_url %}
Code in urls:
(r'^filter_entries/(?P<Entry>.*)/$',
'djangoldap.views.FilterEntriesResponse',
{'filter_template': 'filter_entries.html',
'results_template': 'filter_results.html'}),
(r'^choose_entries/(?P<Entry>.*)/$',
'djangoldap.views.FilterEntriesResponse',
{'filter_template': 'search_entries.html',
'results_template': 'search_results.html'}),
As you can see, those two urls use the same view, but with different templates. How I can force django to retrieve former url, rather than latter?
Name your URLs by adding another item to the tuple:
(r'^choose_entries/(?P<Entry>.*)/$',
'djangoldap.views.FilterEntriesResponse',
{'filter_template': 'search_entries.html',
'results_template': 'search_results.html'},
'sensibleprefix-choose_entries') # <-- this is the name
Then you can use the name in the URL tag.

Django Templates: With Template AutoEscaping on, do I need to pass URLs to URLEncode?

I am running Django trunk and have template Autoescaping on (default). Do I need to pass template URLs to the URLENCODE filter, or does Autoescape take care of that automatically? The Django docs aren't clear.
Django docs say this about Autoescape:
When auto-escaping is in effect, all variable content has HTML escaping applied to it before placing the result into the output (but after any filters have been applied). This is equivalent to manually applying the escape filter to each variable.
Yes, autoescaping is related only to HTML content. URL encoding is another story.