Django template reverse url resolution without creating group for variable - python

I want to create urls like this;
.../film/slug-of-the-film/id-of-film
.../film/id-of-film
example;
../film/fight-club/1040
../film/1040
two links are same.
when I do like this;
url(r'^(?P<slug>[-\w]+/)?(?P<id>[0-9]+)/$', views.summary, name="film_summary")
I can reverse url from template with {% url film_summary film.slug film.id %}
I dont use slug. It is just for readability. So I try something like that;
url(r'^(?:[-\w]+/)?(?P<id>[0-9]+)/$', views.summary, name="film_summary")
but I can't reverse this from template. Is there any way to do that?
Except solutions like this; /film/{{film.slug}}/{{film.id}}
Actually I don't know is it necessary to do like that. I just aimed to don't add a parameter to view function that I won't use.
Thank you in advance :)

I would handle them as separate urls. That also simplifies the regexp for human readers.
url(r'^(?P<id>[0-9]+)/$', views.summary, name="film_summary"),
url(r'^(?P<slug>[-\w]+/)?(?P<id>[0-9]+)/$', views.summary)

Related

Django url dispatcher using repath arguments

In my django app . I have endpoints for a package with the months like :
www.example.com/cart/packagename/months
www.example.com/cart/stater/3
which i dont think will good as an url pattern I want something like :
www.example.com/cart/?package=stater&months=3
And also want to encode the parameters 'package=stater&months=3'
If anyone has any suggestions how to achieve that with django let me know. because before i worked with laravel and its pretty simple to do.
Its also very simple to do in Django. The part after question mark here is called URL Query String. You can get its value by:
def cart_view(request):
packages = request.GET.get('package')
months = request.GET.get('months')
As URL query string has nothing to do with actual URL, so you need to change your url.py to:
path('cart/', cart_view,name='cart_view'),

How to access to dictionary value via variables in another list in Django

I have this situation in my views.py
d = {'05:45':3,'06:30':6 (...) }
T = ['05:45','06:30' (...)]
I would like to get d[T[i]] in my HTML site
I tried to call this with dot, but it doesn't work. I will be thankful for help/hint.
Thing you are trying to do is to get element from dictionary using variable. Am I right ? In django template engine it's a little bit tricky, but possible. Use some template tag from this question to acomplish this
Performing a getattr() style lookup in a django template

How to access a given list element in django templates?

I have a list of values in my view where I want to store some classes name and pass them to the template. Here is the list in the view: menu = ['','disabled','','',''] and my code for the template <li class="{{ menu|slice:"1:2"|first }}"></li>.
So far this is the only working code I came with, is there a better way to retrieve the element menu[2] from this list? what is the proper way in django to callback the element[n] of a list within the template?
menu.1 should work
if you know the exact index of your list, it could be cleaner and more explicit to do retrieval in your view.
context['menu_target'] = menu[1]
instead of having a list of empty values and accessing them in your template
You can just use menu.2 in your template.
An alternative is to get the element[n] that you need in your view, and pass it to the template directly.
Generally speaking, It's a better approch to pass to the template the data that you're going to actually use. So why pass a full list to use only one item of it?

Django Clean URLS and Form Data

I must have some sort of misconception of how GET variables can be manipulated in django, but here goes:
(1) I have a search form that has two required parameters, and one optional parameters.
The form sends to /search/ like so :
<form action="/search/" method="GET">
However, the urls that result from this look something like
http://mylifeforregex:8000/search/?keyword=keyword&columns=name&exact=on
I'm not able to match it in my urlpatterns with the following line :
url(r'^search/(?P<keyword>\w+)(?P<columns>\w+)(?P<exact>\w+)?$', 'home', name='searched')
So if someone could tell me what exactly I'm doing wrong with this regex, it'd be much appreciated.
(2) The more interesting question I have personally is : is there any way I can change the way the form data will be presented? Is it possible to have the form query result in a url that looks like
http://halp:8000/search/keywordquery/columnquery/optionmarked
In this case, none of the querystring parameters that are appended to the URL will match your pattern, because the pattern is simply not the same.
You would need to pick those up in your 'home' view as such:
[variable] = request.GET.get([key])
To get the url pattern you're expecting, you'd have to do a POST to your view that handles the form to get the values, and then redirect to '/search/[keyword]/[column]/[exact]/'

Template tags like Django with Mako and Pylons

For my site need some "widgets" that elaborate output from various data models, because this widgets are visible in any page is possible with mako to retrieve the data without pass (and elaborate) every time with render() in controllers?
May be you need use helpers
in lib/helpers.py
def tweets(**params):
context = {}
return render('tweets.mako', context)
In you page template do this to render you tweets widget:
h.tweets()
It sounds like you're looking for some combination of FormAlchemy, ToscaWidgets and/or Sprox. I would check out those three.
Also, you might read Chapter 6 of http://pylonsbook.com/en/1.1/ . It helped me a bunch; maybe you'll get something out of it as well.

Categories