django-context

Django Context Processors: Is it possible to access current context in ContextProcessor?

北城以北 提交于 2019-12-06 21:09:20
问题 Is there a way I can access current context passed by view in custom context processor so I can add missing variable if I want rather than overriding existing variable ? What I'm trying to Achieve: I construct my URL's like this /city_slug/ and I want to check if city variable already exist in context, otherwise I want to add city to my context (may be using the last used city stored in session variable otherwise default to some city and may be even set session variable for next use.) I think

Django Context Processors: Is it possible to access current context in ContextProcessor?

拜拜、爱过 提交于 2019-12-05 02:20:35
Is there a way I can access current context passed by view in custom context processor so I can add missing variable if I want rather than overriding existing variable ? What I'm trying to Achieve: I construct my URL's like this /city_slug/ and I want to check if city variable already exist in context, otherwise I want to add city to my context (may be using the last used city stored in session variable otherwise default to some city and may be even set session variable for next use.) I think this is very common problem, How do you guys solve it ? You cannot access the current context from

Django Testing - check messages for a view that redirects

允我心安 提交于 2019-12-03 06:50:01
问题 I have been writing tests for one of my django applications and have been looking to get around this problem for quite some time now. I have a view that sends messages using django.contrib.messages for different cases. The view looks something like the following. 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(

Django Testing - check messages for a view that redirects

戏子无情 提交于 2019-12-02 20:27:38
I have been writing tests for one of my django applications and have been looking to get around this problem for quite some time now. I have a view that sends messages using django.contrib.messages for different cases. The view looks something like the following. 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:

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

浪尽此生 提交于 2019-12-01 03:18:27
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 using this serializer and the get_current_user function always returns None . when I debug it, I found that

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

我的梦境 提交于 2019-11-27 13:02:53
I am extending the change_list.html and I in need of outputting a variable defined in settings.py how do I pass that particular variable into the custom admin template context? 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/contrib/admin/#django.contrib.admin.ModelAdmin.changelist_view 来源: https://stackoverflow.com

creating my own context processor in django

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 06:35:20
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_processors.messages", "sandbox.context_processors.say_hello", ) As you can see, I have a module called

What is a context in Django?

一笑奈何 提交于 2019-11-27 03:20:19
I'm a django beginner so I'm trying to understand the concept of context and context processor. What is a context and why do you use it? Is a context a value you need to have available in order to use in a template? Is a context and a context processor the same? I'll apreciate a lot your response. Thanks in advance! Sid When you use a Django Template, it is compiled once (and only once) and stored for future use, as an optimization. A template can have variable names in double curly braces, such as {{ myvar1 }} and {{ myvar2 }} . A Context is a dictionary with variable names as the key and

What is a context in Django?

孤街醉人 提交于 2019-11-26 10:26:56
问题 I\'m a django beginner so I\'m trying to understand the concept of context and context processor. What is a context and why do you use it? Is a context a value you need to have available in order to use in a template? Is a context and a context processor the same? I\'ll apreciate a lot your response. Thanks in advance! 回答1: When you use a Django Template, it is compiled once (and only once) and stored for future use, as an optimization. A template can have variable names in double curly