[Django]-Request object does not have a user Django

6👍

Original answer which assumed a middleware error

This is because you have not enabled the AuthenticationMiddleware

Adds the user attribute, representing the currently-logged-in user, to
every incoming HttpRequest object. See Authentication in Web requests.

Your settings.py ought to have something like this

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
...
)

Updated answer based on stacktrace

You are calling a django view as an ordinary python function here. add_amount clearly isn’t an HttpRequest instance.

 Wallet.objects.filter(username=username).update(add_money(add_amount))

In fact this whole statement doesn’t make sense. In django the standard form is

.update(field_name=new_value)

but you are passing the response of a function call to update here. Were you thinking of

wallet = Wallet.objects.filter(username=username).update(amount = add_money)

Or were you perhaps thinking off adding a number to existing value? Then you will have to use a django F expression.

 wallet = Wallet.objects.filter(username=username).update(amount = F('amount') + add_money)
👤e4c5

0👍

Since you didn’t paste the full stacktrace, it’s difficult to give you an answer 100% correct.

You used request.user in many place of the provided code. request variable should contain a HttpRequest object (since you are in a Django view). Please ensure your request is a HttpRequest and not a string object (unicode)

Leave a comment