Get parameter value in template - python

I have such url pattern:
url(r'^drinks/(?P<drink_name>\D+)/',TemplateView.as_view(template_name='drinks/index.
html'),name="drink")
How can I get access to drink_name parameter value in template.

You can access the variable of the URL in the template as follows:
{{view.drink_name}}
regards

Related

django url accepts None

My url is like this below. it accepts /tracks/1 ,/tracks/2
path('tracks/<int:pk>',views.tracks,name='tracks'),
However I want to accept /tracks without pk value.
How can I do this?
Just add another route to the urlpatterns that returns the same view (or any view you want)
path('tracks/<int:pk>',views.tracks,name='tracks'),
path('tracks/',views.your_view ,name='name'),

Django url path with question mark

I have got a question that I could not find an answer to. Is it possible in Django to create an URL path in urls.py that looks like the following?
path('article?date={some value here}/', article.site.urls)
The query string [wiki] is not part of the path, this is a sequence of key-value pairs.
In the view you can access this with request.GET which is a dictionary-like object.
You can thus define a path as:
path('article/', some_view)
and then in the view access the date with:
def some_view(request):
date = request.GET.get('date')
# …

How can i remove the GET parameters from url in django

I have the form which i am showing by normal view. Then i am send the GET parameters to djnago ChangeList view like django does for lookups like this
student/?region__id__exact=1&status__exact=Published
now is there any way to remove that from the URL in the address bar.
I don't users to see what i am doing
The whole point of GET is that they are retrieved from the URL itself, removing them from the URL removes them entirely.
If you want them 'hidden' you will need to use POST.
The HTTP GET method of form submission passes the information from template to views through URL. If you want to "hide" information from URL use POST instead. In your form do like this:
<form action="/view_name/" method="post">
and in views:
request.POST['name']

Django Build URLs from template with integer param, the primary key

I have this link in a template:
Item 1
and this url in the urls.py
url(r'item/(?P<id>)/$', show_item, name="page_item")
however, this error occurs:
Reverse for 'show_item' with arguments '(63L,)' and keyword arguments '{}' not found.
I looked at this question:
how to get python to not append L to longs or ignore in django template
but it did not help.
Is there another way to use the primary key, which is an integer, in constructing URLs in templates?
The URL name doesn't match. Change the template to be:
Item 1
It should be page_item not show_item in template.

Django: URL doesn't match view function

I have my URL defined as follows:
(r'^article/edit/(.*)/$', 'mysite.views.edit_article')
And the function defined as:
def edit_article(request, article_id):
However, it seems that any request to this page results in the wrong value being passed in for article_id. If I redefine my URL as
(r'^article/(.*)/$', 'mysite.views.edit_article')
Minus the "edit/" it seems to work. Any suggestions on how to fix this?
Try this:
url (r'^article/edit/(?P<article_id>\d+)$', 'mysite.views.edit_article'),
Take a look at the Named Groups in the Django documentation

Categories