Anyone have a quick url dispatcher example? I’ve looked at the Django documentation but I find that there isn’t a complete example that shows you how everything is working in the views.
Specifically, I’m trying to pass the “url” from one view to another. For example, I am trying to make a holiday website: if a button in “available holidays” view called “europe” is clicked, I want to go to http://www.example.com/selected/europe, where all european holidays are displayed
while if a button in the available holidays view called “Australia” is clicked, I want to go to http://www.example.com/selected/australia where similarly australian holidays are displayed.
So far, in url.py I have:
(r'^selected/(?P<location>\w+)/$', 'app.views.selected'),
in views.py I have:
def selected(request, location)
I’m not sure where to go from there.
Also, how are things going to move from the “available holidays” view to the “selected” view…
From the “available holidays” template, you should link to the locations page.
First, add a “name” argument to your url definition:
Then, in the “available holidays” templates, link as such:
Of course, use the location variable if in a forloop:
If location is a model, then you should add a get_absolute_url method to the model e.g.:
Then in the template:
In that case, change the name of the url from ‘selected_holidays’ to ‘location_detail’ if it makes sense – that’s quite a standard in Django.
If you want to be forward compatible, load the future url tag as such:
Then, use a named url string with quotes:
Well I think you got some starters here. You should also overread Django urls manual to know where’s what in case of troubble 🙂 https://docs.djangoproject.com/en/dev/topics/http/urls/