我已经到了一个点,我需要将某些变量传递给我的所有视图(主要是自定义身份验证类型变量).
我被告知写我自己的上下文处理器是最好的方法,但我有一些问题.
我的设置文件如下所示
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.contrib.messages.context_processors.messages",
"sandbox.context_processors.say_hello",
)
Run Code Online (Sandbox Code Playgroud)
如您所见,我有一个名为'context_processors'的模块和一个名为'say_hello'的函数.
看起来像
def say_hello(request):
return {
'say_hello':"Hello",
}
Run Code Online (Sandbox Code Playgroud)
我是否正确地假设我现在可以在我的观点中做以下事情?
{{ say_hello }}
Run Code Online (Sandbox Code Playgroud)
现在,这在我的模板中没有任何内容.
我的观点看起来像
from django.shortcuts import render_to_response
def test(request):
return render_to_response("test.html")
Run Code Online (Sandbox Code Playgroud) 我是一个django初学者,所以我试图理解上下文和上下文处理器的概念.
我会很反应你的回应.提前致谢!
我正在扩展change_list.html,我需要输出settings.py中定义的变量
如何将该特定变量传递到自定义管理模板上下文?
我一直在为我的一个django应用程序编写测试,并且一直在寻找解决这个问题已有一段时间了.我有一个视图,使用django.contrib.messages不同的情况发送消息.该视图类似于以下内容.
from django.contrib import messages
from django.shortcuts import redirect
import custom_messages
def some_view(request):
""" This is a sample view for testing purposes.
"""
some_condition = models.SomeModel.objects.get_or_none(
condition=some_condition)
if some_condition:
messages.success(request, custom_message.SUCCESS)
else:
messages.error(request, custom_message.ERROR)
redirect(some_other_view)
Run Code Online (Sandbox Code Playgroud)
现在,在测试此视图client.get的响应时,不包含context包含messages该视图的字典,因为此视图使用重定向.对于渲染模板的视图,我们可以使用访问消息列表messages = response.context.get('messages').我们如何获得messages重定向视图的访问权限?
在我看来,上下文处理器可以做的一切,中间件都可以做到.那么上下文处理器的意义何在?它们只是中间件 - 精简版吗?
定义FormView派生类时:
class PrefsView(FormView):
template_name = "prefs.html"
form_class = MyForm # What's wrong with this?
def get(self,request):
context = self.get_context_data()
context['pagetitle'] = 'My special Title'
context['form'] = MyForm # Why Do I have to write this?
return render(self.request,self.template_name,context)
Run Code Online (Sandbox Code Playgroud)
我预计context['form'] = MyForm不需要该行,因为form_class已定义,但没有它{{ form }}不会传递给模板.
我做错了什么?
class MyModelSerializer(serializers.ModelSerializer):
field1 = serializers.CharField()
field2 = serializers.SerializerMethodField('get_awesome_user')
def get_current_user(self):
request = self.context.get("request")
if request and hasattr(request, "user"):
return request.user
return None
def get_awesome_user(self, obj):
user = self.get_current_user()
## use this user object, do some stuff and return the value
return ...
Run Code Online (Sandbox Code Playgroud)
我的api(使用authentication_classes和permission_classes)正在使用这个序列化器,get_current_user函数总是返回None.当我调试它时,我发现self.context是空字典,即{}.双重确定我也打印self.context.keys(),仍然是空列表.
我跟着这个帖子.
PS:我正在使用djangorestframework==3.3.3,Django==1.9.1
编辑:添加视图集代码
class MyModelViewSet(viewsets.ModelViewSet):
authentication_classes = (SessionAuthentication, BasicAuthentication, TokenAuthentication)
permission_classes = (IsAuthenticated,)
def list(self, *args, **kwargs):
queryset = MyModel.objects.all() …Run Code Online (Sandbox Code Playgroud) 有没有办法可以访问自定义上下文处理器中视图传递的当前上下文,这样我可以添加缺少的变量,如果我想要而不是覆盖现有变量?
我想要实现的目标:
我建我的网址是这样的/ city_slug /我要检查,如果城市变量在上下文中已经存在,否则我想城市添加到我的上下文(可使用存储在会话变量中最后使用的城市,否则默认一些城市,可能甚至设置会话变量以供下次使用.)
我觉得这是很常见的问题,你们怎么解决呢?
我正在尝试创建一个自定义上下文处理器,它将为登录用户呈现菜单项列表.我做了以下事情:
在我的settings.py中,我有
TEMPLATE_CONTEXT_PROCESSOR = (
'django.contrib.auth.context_processors.auth',
'mysite.accounts.context_processors.user_menu',
)
在帐户子模块context_processors.py下,我现在有以下内容:
def user_menu(request):
return {'user_menu':'Hello World'}
在我的模板页面上,我有以下内容:
{% if user.is_authenticated %}
Menu
{{user_menu}}
{% endif %}
调用视图如下:
def profile(request):
return render_to_response('accounts/profile.html',context_instance=RequestContext(request))
但是我无法{{user_menu}}在页面上呈现任何内容,我知道用户已经过身份验证,因为模板的其他部分具有类似的检查正确呈现.我在这里错过了一些东西.请帮忙谢谢
编辑:谢谢Ben,Daniel,我已修复了(S)TEMPLATE_CONTEXT_PROCESSOR,但Django现在无法解析模块,我得到以下消息
Error importing request processor module django.contrib.auth.context_processors: "No module named context_processors"
更新:我通过改变路径来修复它,django.core.context_processors.auth似乎已经移动了模块
这应该是一个非常简单的.我很确定我以前在我的模板中成功使用了这个上下文来进行链接.我的信念是,这是以某种方式或其他方式构建到RequestContext实例中的.
我的设置文件中的SITE_ID记录是正确的.我为我的所有视图都包含了一个RequestContext实例,并且我已经包含了contrib.auth应用程序,在这种情况下可能是相关的.
{{site}}上下文是以某种方式构建的,还是应该查询实例的Sites对象?
谢谢大家,布兰登