Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update request.user in django?

Tags:

request

django

I have an ajax call which sets request.user.my_field = value

When the ajax succeeds, I reload the page with location.reload(True) I expect the request.user.my_field in the view function is updated now but it has the old value.

How can I fix this?

EDIT

The ajax call:

$.ajax({
    type: 'POST',
    url: '{% url editor_select %}',
    data: {'editor_type':$(this).val(),
           success: function(response_data) {                                                                                                                                                                                                                   
               location.reload(true);                                                                                                                                                                                                                           
           }                                                                                                                                                                                                                                                    
          }                                                                                                                                                                                                                                                     
});     

The first view:

def editor_select(request):
    """                                                                                                                                                                                                                                                                         
    called when user changes editor type to post question/answer                                                                                                                                                                                                                
    """

    editor_type = CharField().clean(request.POST['editor_type'])
    request.user.editor_type = editor_type
    request.user.save()

The second view:

def second_view(request):
    print 'ask, editor_type:', request.user.editor_type

I find AuthenticationMiddleware (which sets request.user to request), doesn't get called in between the ajax call and the location.reload()

so umm???

like image 275
eugene Avatar asked Jul 25 '13 05:07

eugene


1 Answers

Save the model before exiting the view.

request.user.save()
like image 172
Ignacio Vazquez-Abrams Avatar answered Sep 30 '22 15:09

Ignacio Vazquez-Abrams