【问题标题】:How to use {{ request.path }} in django 1.7.7如何在 django 1.7.7 中使用 {{ request.path }}
【发布时间】: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


【解决方案1】:

删除:

"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 中没有提到“OPTIONS”,仅在doc 1.8 中,

如果您不呈现请求,就像文档说的那样:

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

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

【讨论】:

  • 保罗·佩索阿。好的,它有效。谢谢兄弟:D。很高兴认识你。
  • 保罗·佩索阿我做到了。再次感谢:D
猜你喜欢
  • 2023-03-21
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 2018-05-19
  • 2012-04-19
  • 1970-01-01
  • 1970-01-01
  • 2012-06-10
相关资源
最近更新 更多