![](/img/trans.png)
[英]What is the function in Django that calls base.html and how can I see python code inside base.html execute at runtime
[英]How do I display the results of a python function in Django's base.html file?
我具有在views.py
当前定义的以下函数:
def display_path_url(request):
return HttpResponse("Current Path: %s" % request.path)
假设我正在访问127.0.0.1:8000/product_list
,我将打印出product_list/
。
我需要将此信息打印在Django中每个呈现的页面上。 这就是为什么我希望在base.html
调用此函数的原因。
我怎么做?
谢谢您的帮助。
PS:我试图从其他Stackoverlow帖子中找到答案,但找不到任何解决方案。 我希望这个问题不是其他问题的重复。
正如karthikr和Bibhas所提到的,访问模板中的当前request
没有太多要做(假设模板是使用RequestContext
渲染的,请参见https://docs.djangoproject.com/en/dev/ref/templates / api /#subclassing-context-requestcontext )。
作为更一般的答案,如果您想使任何模板都具有功能,那么最好的方法是编写自定义过滤器或模板标签,如此处所述: https : //docs.djangoproject.com/en/dev/howto / custom-template-tags /
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.