'WSGIRequest' object has no attribute 'username' in django
'WSGIRequest' object has no attribute 'username' This is the error I get . I have gone through most of the questions related to it and did some changes accordingly . But didn't meet with the solution yet . In my settings.py this is there
TEMPLATE_CONTEXT_PROCESSORS = ( "django.core.context_processors.debug", "django.core.context_processors.i18n")
TEMPLATE_CONTEXT_PROCESSORS += (
'django.core.context_processors.request',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
)
AUTH_PROFILE_MODULE = "prof.userprofile"
MIDDLEWARE_CLASSES = (
'django.middleware.commo开发者_开发百科n.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware',
)
I want to know what does this error mean , as this is the first time I have come across such an error . And how to debug it ? Edit:
Exception Value:
'WSGIRequest' object has no attribute 'username'
Exception Location: /home/satyajit/geodjango/geographic_admin/prof/views.py in view_foo, line 18
This is my views.py
from django.shortcuts import render_to_response
from django.contrib.gis.shortcuts import render_to_kml
from django import forms
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.contrib.auth.decorators import login_required
@login_required
def view_foo(request):
user1 = User.objects.get(user=request.User.username)
return render_to_response('welcome.html', {})
Edit: ok I just realized how silly this query is and my mistake :D.
If you want the username, you could do something like this:
user1= request.user.username
you get the user instance by request.user
.
this_user = request.user
A little explanation: your initial query does not make much sense, because the User class does not have the field user. Also, this query is superfluous because the user instance is already provided by the request (request.user, which is the user whose session
Errors (and their tracebacks) in python are almost always helpful, and rarely contain irrelevant information.
'WSGIRequest' object has no attribute 'username'
. WSGIRequest
is the class of requests that come in from WSGI requests from the webserver. The convention is to handle these in views as the "request" parameter. So the error indicates that you are doing request.bad_attribute
somewhere, where bad_attribute
in this case is username
.
Your request would only have an attribute named username
if some middleware were active that added it. However, the defaults in django include middleware that adds a user
object, so that's probably what you really wanted. request.user.username
.
Also, remember that attribute access is case-sensitive. It is, after all, Just Python.
精彩评论