Questions tagged [django-context]

django-context refers to the special django.template.Context class used for passing data from the view to the template

171 questions
95
votes
5 answers

creating my own context processor in django

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…
dotty
  • 40,405
  • 66
  • 150
  • 195
61
votes
3 answers

What is a context in Django?

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?
Camilo Sanchez
  • 1,262
  • 2
  • 13
  • 17
56
votes
1 answer

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

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?
James Lin
  • 25,028
  • 36
  • 133
  • 233
30
votes
5 answers

Django Testing - check messages for a view that redirects

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…
Amyth
  • 32,527
  • 26
  • 93
  • 135
26
votes
3 answers

Context processors vs middleware in django

It seems to me that everything a context processor can do, middleware can do. So what's the point of context processors? Are they just middleware-lite?
pseudosudo
  • 6,270
  • 9
  • 40
  • 53
22
votes
1 answer

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

class MyModelSerializer(serializers.ModelSerializer): field1 = serializers.CharField() field2 = serializers.SerializerMethodField('get_awesome_user') def get_current_user(self): request = self.context.get("request") if…
Wendy
  • 1,523
  • 3
  • 12
  • 16
21
votes
3 answers

Django simple_tag and setting context variables

I'm trying to use a simple_tag and set a context variable. I'm using the trunk version of django: from django import template @register.simple_tag(takes_context=True) def somefunction(context, obj): return set_context_vars(obj) class…
neolaser
  • 6,722
  • 18
  • 57
  • 90
16
votes
1 answer

Django FormView does not have form context

When defining a FormView derived class: class PrefsView(FormView): template_name = "prefs.html" form_class = MyForm # What's wrong with this? def get(self,request): context = self.get_context_data() …
tonjo
  • 1,394
  • 1
  • 14
  • 27
10
votes
4 answers

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

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…
Srikanth Chundi
  • 897
  • 9
  • 8
8
votes
2 answers

Django {{site}} template context not working?

This should be a super simple one. I'm pretty sure that I've used this context successfully in the past in my templates for linking purposes. My belief was that this was built into the RequestContext instance somehow or other. the SITE_ID record in…
Ben
  • 15,010
  • 11
  • 58
  • 90
8
votes
1 answer

Unable to get custom context processor to be invoked

I am trying to create a custom context processor which will render a list of menu items for a logged in user. I have done the following: Within my settings.py I have TEMPLATE_CONTEXT_PROCESSOR = ( …
VDev
  • 2,287
  • 5
  • 25
  • 27
7
votes
3 answers

Accessing global variable in view using context processor in Django

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 -…
laszchamachla
  • 763
  • 9
  • 21
7
votes
1 answer

django access context in template

My code is like this: I custom my context and want to access my query set in template class GetStudentQueryHandler(ListView): template_name = 'client.html' paginate_by = STUDENT_PER_PAGE context_object_name = 'studentinfo' def…
Roger Liu
  • 1,768
  • 3
  • 16
  • 25
6
votes
1 answer

How to add extra context & queryset field in Django generic views?

I'm building Django application, with submitting links and voting functionality. I want to show all links, voted by a user in user details page. I can retrieve them in python shell using this: Link.objects.filter(votes__voter=user) But I don't know…
6
votes
1 answer

Django not able to render context when in shell

This is what I am trying to run. When I run the server and run these lines within a view and then return an HttpResponse, then everything goes fine. However when I run python manage.py shell and then try to run through these lines then I get an…
Chase Roberts
  • 9,082
  • 13
  • 73
  • 131
1
2 3
11 12