如何在 Django 1.7.7 中使用 {{ request.path }}
How to use {{ request.path }} in django 1.7.7
我在 django 1.7.7 中遇到了一些问题。我不能在 django 1.7.7 的模板中使用 {{ request.path }},但在 django 1.6 中,我可以这样做。
setting.py 中的配置模板以在 django 1.7.7 中使用 {{ request }}:
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [
os.path.join(PACKAGE_ROOT, "templates"),
],
"APP_DIRS": True,
"OPTIONS": {
"debug": DEBUG,
"context_processors": [
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.core.context_processors.request",
"django.contrib.messages.context_processors.messages",
"pinax_theme_bootstrap.context_processors.theme",
],
},
},]
在 django 1.6 中:
TEMPLATE_CONTEXT_PROCESSORS = [
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.core.context_processors.request",
"django.contrib.messages.context_processors.messages",
"pinax_theme_bootstrap.context_processors.theme",
"allauth.account.context_processors.account",
"django.core.context_processors.request"]
还有我的模板html
{% extends "base.html" %}
{% load i18n %}
{% load url from future %}
{% block body_class %}home{% endblock %}
{% block body_base %}
{{ request.path }}
<div class="row">
<form class="answer-question-form" method="POST" action="/add_answer_question">
{% csrf_token %}
<div class="col-md-12" style="padding-left: 0px; padding-right: 0px">
<div class="form-group col-md-12">
<label>Question</label>
<input type="text" class="form-control" name="question" placeholder="Question...">
</div>
<div class="form-group col-md-6 col-sm-12">
<label>Choose type question</label>
<select class="form-control" name="categories">
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
</select>
</div>
<div class="form-group col-md-12">
<label>Type answer</label>
<ul class="nav nav-pills nav-justified choose-type-answer">
<li class="active"><a class="type-answer" href="#" id="type-answer-01">Radio</a></li>
<li><a class="type-answer" href="#" id="type-answer-02">Check</a></li>
<li><a class="type-answer" href="#" id="type-answer-03">Seek</a></li>
</ul>
<input name="type-answers" type="hidden" value="01">
</div>
<div class="form-group answers col-md-12">
<label>Answers</label>
<input name="answer" type="text" class="form-control answer-detail" placeholder="Input answer here...">
<input name="answer" type="text" class="form-control answer-detail" placeholder="Input answer here...">
</div>
<div class="form-group col-md-12">
<button class="btn btn-default pull-right" id="submit">Submit</button>
</div>
</div>
</form>
</div>
{% endblock %}
因此,我将 "django.core.context_processors.request" 添加到模板,但它不起作用。
请帮帮我!谢谢
删除:
"OPTIONS": {
"debug": DEBUG,
"context_processors": [
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.core.context_processors.request",
"django.contrib.messages.context_processors.messages",
"pinax_theme_bootstrap.context_processors.theme",
],
},
添加:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages"
"django.core.context_processors.request",
)
documentation 1.7, they mention the "OPTIONS", only in the doc 1.8、
无处可寻
如果您没有呈现请求,如文档所述:
If you’re using Django’s render_to_response() shortcut to populate a
template with the contents of a dictionary, your template will be
passed a Context instance by default (not a RequestContext). To use a
RequestContext in your template rendering, use the render() shortcut
which is the same as a call to render_to_response() with a
context_instance argument that forces the use of a RequestContext.
可能问题出在你的观点上。尝试使用 render 而不是 render_to_response (如果你使用它)
我在 django 1.7.7 中遇到了一些问题。我不能在 django 1.7.7 的模板中使用 {{ request.path }},但在 django 1.6 中,我可以这样做。
setting.py 中的配置模板以在 django 1.7.7 中使用 {{ request }}:
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [
os.path.join(PACKAGE_ROOT, "templates"),
],
"APP_DIRS": True,
"OPTIONS": {
"debug": DEBUG,
"context_processors": [
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.core.context_processors.request",
"django.contrib.messages.context_processors.messages",
"pinax_theme_bootstrap.context_processors.theme",
],
},
},]
在 django 1.6 中:
TEMPLATE_CONTEXT_PROCESSORS = [
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.core.context_processors.request",
"django.contrib.messages.context_processors.messages",
"pinax_theme_bootstrap.context_processors.theme",
"allauth.account.context_processors.account",
"django.core.context_processors.request"]
还有我的模板html
{% extends "base.html" %}
{% load i18n %}
{% load url from future %}
{% block body_class %}home{% endblock %}
{% block body_base %}
{{ request.path }}
<div class="row">
<form class="answer-question-form" method="POST" action="/add_answer_question">
{% csrf_token %}
<div class="col-md-12" style="padding-left: 0px; padding-right: 0px">
<div class="form-group col-md-12">
<label>Question</label>
<input type="text" class="form-control" name="question" placeholder="Question...">
</div>
<div class="form-group col-md-6 col-sm-12">
<label>Choose type question</label>
<select class="form-control" name="categories">
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
</select>
</div>
<div class="form-group col-md-12">
<label>Type answer</label>
<ul class="nav nav-pills nav-justified choose-type-answer">
<li class="active"><a class="type-answer" href="#" id="type-answer-01">Radio</a></li>
<li><a class="type-answer" href="#" id="type-answer-02">Check</a></li>
<li><a class="type-answer" href="#" id="type-answer-03">Seek</a></li>
</ul>
<input name="type-answers" type="hidden" value="01">
</div>
<div class="form-group answers col-md-12">
<label>Answers</label>
<input name="answer" type="text" class="form-control answer-detail" placeholder="Input answer here...">
<input name="answer" type="text" class="form-control answer-detail" placeholder="Input answer here...">
</div>
<div class="form-group col-md-12">
<button class="btn btn-default pull-right" id="submit">Submit</button>
</div>
</div>
</form>
</div>
{% endblock %}
因此,我将 "django.core.context_processors.request" 添加到模板,但它不起作用。
请帮帮我!谢谢
删除:
"OPTIONS": {
"debug": DEBUG,
"context_processors": [
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.core.context_processors.request",
"django.contrib.messages.context_processors.messages",
"pinax_theme_bootstrap.context_processors.theme",
],
},
添加:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages"
"django.core.context_processors.request",
)
documentation 1.7, they mention the "OPTIONS", only in the doc 1.8、
无处可寻如果您没有呈现请求,如文档所述:
If you’re using Django’s render_to_response() shortcut to populate a template with the contents of a dictionary, your template will be passed a Context instance by default (not a RequestContext). To use a RequestContext in your template rendering, use the render() shortcut which is the same as a call to render_to_response() with a context_instance argument that forces the use of a RequestContext.
可能问题出在你的观点上。尝试使用 render 而不是 render_to_response (如果你使用它)