在django模板中显示查询字符串值

use*_*099 6 django templates query-string

我想在get方法中将一些成功消息打印回索引页面(home.html),即在查询字符串的帮助下打印模板页面.我已经使用重定向到索引页面

    return HttpResponseRedirect("/mysite/q="+successfailure)
Run Code Online (Sandbox Code Playgroud)

现在我想在索引文件(或模板文件/ home.html文件)中打印字符串成功以及其他内容.

我搜索了解决方案,发现应该在设置中添加"django.core.context_processors.request context".但我找不到添加它的地方.我目前正在使用python 2.7和django 1.4.3.

我也试过用

    render_to_response("home.html",{'q':successfailure})
Run Code Online (Sandbox Code Playgroud)

然而,结果打印在当前页面(addContent.html-我不想要)但我想将网址发送到'/ mysite /'并在那里打印结果.

请建议适当的解决方案.提前致谢.

Fra*_*llo 18

这些是默认的上下文处理器:https: //docs.djangoproject.com/en/dev/ref/settings/#template-context-processors

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",
    #add this
    "django.core.context_processors.request" 
)
Run Code Online (Sandbox Code Playgroud)

如果它不在你的设置中而不是你还没有覆盖它.现在就这样做.

然后在你的模板中,这样的事情:

{% if request.GET.q %}<div>{{ request.GET.q }}</div>{% endif %}
Run Code Online (Sandbox Code Playgroud)

另外,我注意到你的链接url你没有使用查询字符串运算符?应该是:

return HttpResponseRedirect("/mysite/?q="+successfailure)
Run Code Online (Sandbox Code Playgroud)

  • "django.core.context_processors.request"现在是"django.template.context_processors.request" (4认同)