I use the url template tag with named URLs all the time in Django but now I need to be able to pass query strings to a couple of URLs in a Django template. Is this possible to do? The url template tag documentation does not mention GET query strings at all and hardcoding in URLs is bad practice if you ever want to change the URL in the future you also need to change the templates.
Well, I do not quite understand but... I think you want something like this:
<a title="" href="{% url my_url %}?foo=bar&spam=eggs">My link</a>
Related
I know this is an armature question but here goes.
I have a url path as follows:
path('projects/<s>', PL.projects),
And I pass a string from the html template by putting it into an href tag like so projects/some_string. this works once but then the base url changes to <ip>/projects/some_string. so when I try to excite the path to pass a string in that domain then I get an error as the url is now <ip>/projects/projects/some_string.
How do I set it up so that I can pass as many strings as possible as many times as possible without having to clean my url in the browser everytime.
Django has inbuilt url lookup features
path("some_random_url_link_1/", views.Link1View.as_view(), name="url_link_1"),
path("some_random_url_link_2/<int:some_id>/<slug:some_slug>/", views.Link2View.as_view(), name="url_link_2"),
in your template you can use it like this, and pass variables/parameters like this. FYI you don't need to use {{variable}} tag here
<a href="{% url 'url_link_1' %}" >Link 1</a>
<a href="{% url 'url_link_2' some_id=id1 some_slug=random_slug %}" >Link 2</a>
Learn how to use the reverse() function and the url template tag and your problems will be gone.
Those functions are built-in with Django and can handle all of that nasty URL stuff.
Reverse: https://docs.djangoproject.com/en/3.2/ref/urlresolvers/
Url Template tag: https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#url
For part of my user interface I need a URLencoded URL.
This code doesn't work, but it's intended to give an impression of what I'd like to do:
{% url 'team_event_calendar' id=team.id | urlencode %}
Basically, get a particular URL - in this case "team_event_calendar" for a particular team, and then URLencode it.
I suppose I could make a new version of the URL tag which URLencodes everything, but is there a way to do it with just Django's built-in tags?
Using django1.9
my urls would look something like example.com/my_url/cachebuster
in my email.html template I have something like
{% url my_url cachebuster %}
Now cachebuster is actually "%%cachebuster%%"
This is a substitution string which my email api is parsing for, and whenever it finds "%%cachebuster%%" it replaces it with the specific users cachebuster.
My issue is that the django {% url %} function automatically encodes that string to "%25%25cachebuster%25%25"
Which means the api never finds "%%cachebuster%%" and never replaces it
So instead of a dynamic url, its always example.com/my_url/%25%25cachebuster%25%25
How can I fix this, without changing the substitution string? I've tried using mark_safe but haven't got it to work
The only way I can think of is to hardcode /my_url/{{cachebuster}} without using the django url tag, but this is bad practice because the url templatetag was created for a reason
Let's assume I am building a blogging app and create the following url pattern:
url(r'^(?P<category>.+?)/(?P<date>.+)$', views.post_list, name='post_list'),
Then I create multiple templates adding this everywhere:
{% url 'myapp:post_list' category date %}
But then I think hmm... I don't want date there, let it be <category> and <slug> instead.
Then I have to change corresponding {% url tags everywhere in my templates!
Wouldn't it be better to rewrite url regex like this:
url(r'^(?P<url>.+?/.+)$', views.post_list, name='post_list'),
and define some url_split() function somewhere, which would parse it in the views, add url() method or property to corresponding model and be able to use the following url tags:
{% url 'myapp:post_list' obj.url %}
and therefore never touch them in case I want to change something in my url regex/parameters etc?
Is it good design or am I missing something?
But then I think hmm... I don't want date there, let it be
and instead.
Then I have to change corresponding {% url tags everywhere in my
templates!
Why is this a problem?
Is it good design
No it is not:
The function becomes ambiguous. It is doing too many things at once.
You are replicating functionality already available in the framework.
You have now effectively creating a choke point by creating a "blanket" catch-all request method. In this method there will be a bunch of decision tree if/else clauses and such branching is prime spot for bugs.
The entire purpose of doing this is for some imagined use case that may or may not happen.
From xkcd # 974:
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.