如何在django 1.7.7

时间:2015-10-09 02:52:35

标签: python django templates request django-1.7

我在django 1.7.7中遇到了一些问题。我不能在django 1.7.7的模板中使用{{request.path}},但在django 1.6中,我可以这样做。

在setup.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 %}
&#13;
&#13;
&#13;

所以,我添加了#34; django.core.context_processors.request&#34;到模板,但它不起作用。

请帮助我!感谢

1 个答案:

答案 0 :(得分:2)

删除:

"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中没有任何地方,只提到doc 1.8中的“选项”,

如果您没有呈现请求,就像文档说:

  

如果您正在使用Django的render_to_response()快捷方式来填充   模板与字典的内容,您的模板将是   默认情况下传递一个Context实例(不是RequestContext)。用一个   在模板渲染中使用RequestContext,使用render()快捷方式   这与使用a调用render_to_response()相同   context_instance参数,强制使用RequestContext。

也许问题出在你的观点上。尝试使用render而不是render_to_response(如果您使用它)