我是django和编程的新手。我正在尝试建立一个电子商务网站,但仍停留在自定义中间件上。我正在尝试设置一条消息,该消息将出现在网站的每个页面上。
我认为代码是有效的,因为如果我在终端窗口中输入print(“ something”)并刷新浏览器“ something”。
middleware.py
from .models import MarketingMessage
class DisplayMarketing:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
return response
def process_view(self, request, view_func, view_args, view_kwargs):
try:
request.session["marketing_message"] = MarketingMessage.objects.all()[0].message
except:
request.session["marketing_message"] = False
settings.py
MIDDLEWARE = [
...
'marketing.middleware.DisplayMarketing',
]
base.html
{% if marketing_message %}
<div class="alert alert-top-message alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert">
<span aria-hidden="true">×</span><span class="sr-only">Close</span>
</button>
<div class="container">
<h3> {{ marketing_message }} </h3>
</div>
</div>
{% endif %}
我希望每页顶部都显示一条警报消息(您好,在那里)
有人可以帮忙吗?