python - How would I pass a calculated parameter to a url in Django -


i have parameters calculated in django view need passed url. how able that.

the current url pattern:

url(r'^hotels/(?p<country>[a-za-z0-9-]+)/(?p<region>[a-za-z0-9-]+)', hotel.views.hotels, name='hotels') 

the link template:

<a href="{% url 'hotels' %}?region_id={{ region.id }}">{{ region.name }}</a> 

and view: region_id request.

def hotels(request):     region_id = request.get.get('region_id')     region = region.objects.get(id=region_id)     region_name = region.name     country_name= region.country.name      context = {         'region_id': region_id,         'region': region_name,         'country': country_name,     }     return render(request, 'hotel/hotels.html', context) 

i region_id template, after use query region, region name , corresponding country.
region_name , country_name should used in url.

until not seem work.

i hope me done.

,

your hotels url expect 2 arguments: country , region need pass them url function:

<a href="{% url 'hotels' region.country.name region.id %}">{{ region.name }}</a> 

next in view, take them incoming kwargs.

def hotels(request, country, region):     pass 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -