Add this code to you your context_processors.py in your project and then install it in your settings.py TEMPLATE_CONTEXT_PROCESSORS. In your template you can print out a segment of a url by using {{ segment_1 }}. For example if you're on the page "/mysite/section1/section2/" and you used {{ segment_2 }} it would print section1.
This idea was taken from Expression Engines URL Segments, http://expressionengine.com/docs/templates/globals/url_segments.html.
This comes in handy if you only want to do something in your template if the page your on has a particular segment.
FYI, I haven't used this in a production setting yet so it could be buggy still.
1 2 3 4 5 6 7 8 9 10 11 12 13 | def url_segments(request):
from urlparse import urlparse
# convert the url path into a list
url_segment = request.path_info.strip("/").split("/")
# assign each segment to it's on var
url_segment_dict = {}
for i in range(len(url_segment)):
url_segment_dict["segment_%d" % (i+1)] = url_segment[i]
return url_segment_dict
|
More like this
- find even number by Rajeev529 2 weeks, 1 day ago
- Form field with fixed value by roam 1 month ago
- New Snippet! by Antoliny0919 1 month, 2 weeks ago
- Add Toggle Switch Widget to Django Forms by OgliariNatan 4 months ago
- get_object_or_none by azwdevops 7 months, 3 weeks ago
Comments
Please login first before commenting.