Get request.session from a class-based generic view

You have access to self.request from anywhere within the class (and therefore self.request.session) https://docs.djangoproject.com/en/dev/topics/class-based-views/generic-display/#dynamic-filtering The key part to making this work is that when class-based views are called, various useful things are stored on self; as well as the request (self.request) this includes the positional (self.args) and name-based (self.kwargs) arguments captured according to the URLconf.

django-admin: Add extra row with totals

Yes, you can do it in many ways, but most django-ist way to do is: First override the default django listing view… And give a new template file directory ModelAdmin.changelist_view(self, request, extra_context=None) Like: class MyModelAdmin(admin.ModelAdmin): # A template for a very customized change view: change_list_template=”admin/myapp/extras/sometemplate_change_form.html” def get_total(self): #functions to calculate whatever you want… total = … Read more

Django Passing data between views

There are different ways to pass data between views. Actually this is not much different that the problem of passing data between 2 different scripts & of course some concepts of inter-process communication come in as well. Some things that come to mind are – GET request – First request hits view1->send data to browser … Read more

Execute code in Django after response has been sent to the client

The method I am going for at the moment uses a subclass of HttpResponse: from django.template import loader from django.http import HttpResponse # use custom response class to override HttpResponse.close() class LogSuccessResponse(HttpResponse): def close(self): super(LogSuccessResponse, self).close() # do whatever you want, this is the last codepoint in request handling if self.status_code == 200: print(‘HttpResponse successful: … Read more

AssertionError: `HyperlinkedIdentityField` requires the request in the serializer context

You’re getting this error as the HyperlinkedIdentityField expects to receive request in context of the serializer so it can build absolute URLs. As you are initializing your serializer on the command line, you don’t have access to request and so receive an error. If you need to check your serializer on the command line, you’d … Read more

Django internationalization language codes [closed]

If you want something you can use from within django, try: from django.conf import settings this will be in the format above, making it perfect for assignment in one of your models choices= fields. (i.e. user_language = models.CharField(max_length=7, choices=settings.LANGUAGES)) LANGUAGES = ( (‘ar’, gettext_noop(‘Arabic’)), (‘bg’, gettext_noop(‘Bulgarian’)), (‘bn’, gettext_noop(‘Bengali’)), etc…. ) Note about using settings: Note … Read more

__init__() got an unexpected keyword argument ‘user’

You can’t do LivingRoom.objects.create(user=instance) because you have an __init__ method that does NOT take user as argument. You need something like #signal function: if a user is created, add control livingroom to the user def create_control_livingroom(sender, instance, created, **kwargs): if created: my_room = LivingRoom() my_room.user = instance Update But, as bruno has already said it, … Read more