[Django]-Pass kwargs for the view in reverse; django 1.5

8👍

You could use this:

If this was your url:

url(r'(?P<category>[a-z]+)$', 'display', name='dyn_display')
reverse('dyn_display', kwargs={'category': 'first'})

To redirect you can use it like this in your view:

from django.http import HttpResponseRedirect
return HttpResponseRedirect(reverse('dyn_display', kwargs={'category': 'first'}))

If this was your url:

url(r'$', 'display', name='dyn_dysplay')
reverse('dyn_display')

To redirect you can use it like this in your view:

from django.http import HttpResponseRedirect
return HttpResponseRedirect(reverse('dyn_display'))

To have a view that could receive an optional value you would need 2 urls:

url(r'$', 'display', name='dyn_optional_display')    
url(r'(?P<category>[a-z]+)$', 'display', name='dyn_display')

And then your view:

def courses_display(request, category=None):
    ctx = {}
    if category:
        ctx.update({category: 'in'})
    return render_to_response('display/basic.html', ctx,    context_instance=RequestContext(request))
👤CJ4

Leave a comment