django-context

Accessing global variable in view using context processor in Django

北城以北 提交于 2019-12-30 09:32:49
问题 Assuming I have a context processor: def title(request): return {'titles': 'mytitle'} I can access this variable in template as {{ titles }} . But how can I do so in a view? def myview(request): print request.titles doesn't seem to work - 'WSGIRequest' object has no attribute 'titles' Or maybe there is a better approach (than context processors) to have global variables accessible in both views and templates? Thanks in advance. 回答1: Context processors aren't in any way global variables. They

Django how to pass custom variables to context to use in custom admin template?

这一生的挚爱 提交于 2019-12-28 02:49:13
问题 I am extending change_list.html and I need to output a variable defined in settings.py. How do I pass that particular variable into the custom admin template context? 回答1: class MyModelAdmin(admin.ModelAdmin): ... def changelist_view(self, request, extra_context=None): extra_context = extra_context or {} extra_context['some_var'] = 'This is what I want to show' return super(MyModelAdmin, self).changelist_view(request, extra_context=extra_context) See: https://docs.djangoproject.com/en/dev/ref

Django TEMPLATE_CONTEXT_PROCESSORS are being called too many times

点点圈 提交于 2019-12-23 19:59:00
问题 I need show some statistic numbers in all pages, so I decided to use context processors. But I just figured out that my function is being called 2 to 7 times every page load. I am doing 4 queries inside the function, so I am getting a very bad performance. Every page load it can take up to 28 (4*7) queries... I would like to know why this is happening and what can I do to avoid it. settings.py TEMPLATE_CONTEXT_PROCESSORS = ( 'django.contrib.auth.context_processors.auth', 'django.core.context

Django rest framework - self.context doesn't have request attribute

 ̄綄美尐妖づ 提交于 2019-12-19 05:16:43
问题 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 ... My api(which uses authentication_classes and permission_classes ) is

creating my own context processor in django

余生长醉 提交于 2019-12-17 07:12:30
问题 I have come to a point where I need to pass certain variables to all of my views (mostly custom authentication type variables). I was told writing my own context processor was the best way to do this, but I am having some issues. My settings file looks like this 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

django-postman discards RequestContext

对着背影说爱祢 提交于 2019-12-12 03:08:18
问题 I installed django-postman to my project. Afterwards, I saw that when I login, it logs in to the user home but then whenever I click on a link, session goes out. It wants me to re-login. I'm using context_instance=RequestContext(request) at each view. So what could be the problem? This happened when I inserted the following: TEMPLATE_CONTEXT_PROCESSORS = ( 'django.core.context_processors.static', 'django.core.context_processors.request', ) Without static one, it event doesnt recognize STATIC

Django template tag exception

二次信任 提交于 2019-12-11 21:53:19
问题 It looks like this template tag works like a charm for most people: http://blog.localkinegrinds.com/2007/09/06/digg-style-pagination-in-django/ For some reason I get this error: Caught an exception while rendering: 'is_paginated' I use this template tag in my template like so: {% load digg_paginator %} {% digg_paginator %} Where digg_paginator.py is in my app/templatetags folder and the included template context digg_paginator.html is in my app/templates folder. The queryset that needs

Setting initial formfield value from context data in Django class based view

空扰寡人 提交于 2019-12-11 00:47:26
问题 I've got an activation url that carries the activation key ( /user/activate/123123123 ). That works without any issue. get_context_data can plop it into the template fine. What I want to do is have it as an initial value for the key field so the user only needs to enter username and password as created at registration. How can I pull the key from context or get() without hardcoding the field into the template? class ActivateView(FormView): template_name = 'activate.html' form_class =

Django custom context processor being called twice per request

我与影子孤独终老i 提交于 2019-12-08 06:35:49
问题 I created a simple custom context processor that needs to only be run once per request. After putting in some logging hooks I found that it is being called twice per request. Is this a known "feature" that a missed in the docs? Is it related to the number of templates in the inheritance tree? Is it a bug in 1.03? 回答1: This is not expected behavior. Context processor are executed once each time a RequestContext is instantiated). In the case of template inheritance, the same context instance is

When to use context processor

本秂侑毒 提交于 2019-12-08 02:41:52
问题 I have a site on which I'm including a userBox with some data shown for each logged user (your name, avatar etc). From what I already know about django it seems obvious, that I should add query for user to context processor (so that I can use {{user}} variable in this included userBox ). But while using django-lfs shop I've noticed, that it's templates are using {{ user }} variable which is nowhere added to context processors nor template tags. Is there any other way to obtain user in my