Chris Meek Chris Meek - 10 months ago 30
Python Question

Django getting variable from URL into view

Is it possible to get a variable in a URL to pass to a view. For example. I have a location table with many locations. If I click a location it goes to a details page of that location. The location ID gets added to the URL. I then have a link to another page/view but want to reference the location id in the next view.

url(r'^locations/get/(?P<location_id>\d+)/$', 'assessments.views.location'),
url(r'^locations/get/(?P<location_id>\d+)/next_page/$', 'assessments.views.next_page'),

def location(request, location_id=1):
return render_to_response('dashboard/location.html', {'location': Location.objects.get(id=location_id) })

def next_page(request):
loc = Location.objects.get(id='id of that location')


Simply access it in the view function with its name

def next_page(request,location_id):
    loc = Location.objects.get(id=location_id)