I have the following function that is currently defined in my views.py
:
def display_path_url(request):
return HttpResponse("Current Path: %s" % request.path)
Say I am accessing 127.0.0.1:8000/product_list
, I will have product_list/
printed.
I need this information to be printed on every rendered page in Django . This is why I want this function to be called in base.html
.
How do I do that?
Thank you for your help.
PS: I tried to find an answer from other Stackoverlow posts but could not find any solutions. I hope this question is not a duplicate of some other question.
request
variable is available in Django templates. You can access it from anywhere. Not only base.html
. Like this -
{{ request.path }}
As karthikr and Bibhas already mentionned you don't have much to do to access the current request
in a template (assuming the template is rendered using a RequestContext
, cf https://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext ).
As a more general answer, if you want to make a function available from any template, then the best way is to write a custom filter or template tag as explained here : https://docs.djangoproject.com/en/dev/howto/custom-template-tags/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.