gpt4 book ai didi

django - 在 Django 模板中访问 RequestContext?

转载 作者:行者123 更新时间:2023-12-01 12:51:03 25 4
gpt4 key购买 nike

我似乎无法访问我的 Django 模板中的请求对象。

这是我的 settings.py 文件的一部分:

import django.conf.global_settings as DEFAULT_SETTINGS
TEMPLATE_CONTEXT_PROCESSOR = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
'django.core.context_processors.request',
)

网址.py

urlpatterns = patterns('',
url(r'^event/create/$', EventCreateView.as_view(), name='create_event'),
url(r'^event/update/(?P<pk>\d+)/$', EventUpdateView.as_view(), name='update_event'),
url(r'^event/delete/(?P<pk>\d+)/$', EventDeleteView.as_view(), name='delete_event'),
)

View .py

from django.views.generic import CreateView, UpdateView, DeleteView
from events.models import Event
from events.forms import EventForm

class EventCreateView(CreateView):
model = Event
form_class = EventForm

class EventUpdateView(UpdateView):
model = Event
form_class = EventForm

class EventDeleteView(DeleteView):
model = Event

表单.py

from django.forms import ModelForm
from events.models import Event

class EventForm(ModelForm):
class Meta:
model = Event

event_form.html(用于 CreateView/UpdateView)

{% block content %}
<form action='{{ request.get_full_path }}' method='post'>
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type='submit' value='Create event!' />
</form>
{% endblock %}

上面,request.session 好像什么都没做。我试过查看文档和类似的问题,但似乎没有任何效果。有什么想法吗?

提前致谢。

最佳答案

您有错字,TEMPLATE_CONTEXT_PROCESSORS 末尾缺少“S”,也许只是在您的问题中?设置应该是:

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",)

,不是 TEMPLATE_CONTEXT_PROCESSOR = ...。我更喜欢完全覆盖该设置,以便清楚什么是事件的(例如,您可能想在生产中禁用调试)。

关于django - 在 Django 模板中访问 RequestContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12398626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com