我的问题类似于Same URL in multiple views in Django,但我不是基于用户身份验证来呈现模板,而是基于浏览器中启用或禁用的JavaScript。在
我想做什么?
如果在浏览器中启用了JavaScript,我将尝试呈现index.html
页面,否则,如果禁用了jsDisabled.html
页面,并且这两个页面都应该在相同的URL模式下呈现,例如:
如果在浏览器中启用了JavaScript,localhost:8000
应该呈现index.html
,或者如果JavaScript被禁用,它应该呈现jsDisabled.html
页面。在
注意:我正在检查JavaScript是否在浏览器中被禁用,方法是使用在JavaScript被禁用时运行的<noscript>
标记。
以下是我目前为止的代码:
基本.html
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
</head>
<body class="noJs">
...
<a href="{% url 'index' 0 %}"> abc </a>
...
<noscript>
<style type="text/css">
.noJs {
display: none;
}
</style>
<meta http-equiv="refresh" content="{% ulr 'index' 1 %}"> /* This should render jsDisabled.html page on the same URL which is 'localhost:8000' */
</noscript>
</body>
</html>
网址.py:
^{pr2}$视图.py:
from django.shortcuts import render
def index(request, flag):
if (flag):
return render(request, 'jsDisabled.html')
else:
return render(request, 'index.html')
经过大量调试,我终于找到了解决方案:)这就是:
在基本.html:
在网址.py
^{pr2}$在视图.py:
我在templates文件夹中创建了一个名为
includes
的文件夹,并将jsDisabled.html
放在其中,从而将include内置模板标记作为{% include 'includes/jsDisabled.html' %}
建议的{% include 'includes/jsDisabled.html' %}
。在根据您的Django版本,您可能需要指定TEMPLATE_DIR=in设置.py(在较新版本中,这是不需要的)。在
下面是一些关于模板和标记逻辑的有用的information:
在基本.html:
^{pr2}$在索引.html:
相关问题 更多 >
编程相关推荐