【发布时间】:2016-01-06 20:42:00
【问题描述】:
我在 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”,但它不起作用。
请帮帮我!谢谢
【问题讨论】:
-
你能显示一个视图代码吗?
-
我只是将 {{ reuqest.path }} 放在 html 中,但是当我使用 django 1.7.7 时,我在屏幕上看不到任何东西。但在 django 1.6 中,我可以在模板中看到当前 url
-
我知道,但是您可以通过查看示例代码来更新问题吗?我认为问题可能在视图中。
-
@DuyTrần 请向我们展示您在哪里渲染模板。
-
@SANDHYALALKUMAR 我明白,但我不想这样做
标签: python django templates request django-1.7