Reputation: 675
Being a django starter, I have a little problem to deal with:
<li><a href="{% url 'dyn_display' category='first'%}">first</a></li>
<li><a href="{% url 'dyn_display' category='second'%}">second</a></li>
<li><a href="{% url 'dyn_display' category='third'%}">third</a></li>
url(r'(?P<category>[a-z]+)$', 'display', name='dyn_display')
def courses_display(request, category):
return render_to_response('display/basic.html', {category: 'in'}, context_instance=RequestContext(request))
and finally a part of basic.html, so you can get a thought of why i need that category variable
<div class="accordion" id="accordion2">
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseFirst">
<strong>First</strong>
</a>
</div>
<div id="collapseFirst" class="accordion-body collapse {{ first }}">
<div class="accordion-inner">
...
</div>
</div>
</div>
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseSecond">
<strong>Second</strong>
</a>
</div>
<div id="collapseSecond" class="accordion-body collapse {{ second }}">
<div class="accordion-inner">
...
</div>
</div>
</div>
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseThird">
<strong>Third</strong>
</a>
</div>
<div id="collapseThird" class="accordion-body collapse {{ third }}">
<div class="accordion-inner">
...
</div>
</div>
</div>
</div>
Since the whole page is done with bootstrap and Jquery, last parts of url
some/url/first
some/url/second
some/url/third
are kind of ugly, since the accordion element lets me collapse and open respective inner bodies without reloading the page. So the question is: is there a way to pass a kwarg in reverse, that's going to be used in a view, not to determine the url in reverse()?
the way I get url like some/url/ and, still passing kwarg from the first .html page to the view?
Upvotes: 3
Views: 7547
Reputation: 2535
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))
Upvotes: 8