I’m relatively new to django..
In the app that I’m building, there are multiple types of users (ie User1, User2, User3) that are all inheriting from django.contrib.auth.models.User and upon login, each user should be redirected to a success page depending on what type of user they are.
In views.py:
def login_attempt(request):
user = request.user
data = {}
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
try:
User1.objects.get(username = user.username)
type = "undergrad"
except ObjectDoesNotExist:
pass
try:
User2.objects.get(username = user.username)
type = "grad"
except ObjectDoesNotExist:
pass
try:
User3.objects.get(username = user.username)
type = "sponsor"
except ObjectDoesNotExist:
pass
return render_to_response (
"templates/success_"+type+".html",
data,
context_instance=RequestContext(request)
)
else:
return render_to_response (
"templates/fail1.html",
data,
context_instance=RequestContext(request)
)
else:
return render_to_response (
"templates/fail2.html",
data,
context_instance=RequestContext(request))
and type(user) is <class 'django.contrib.auth.models.User'>
I’m currently running tests via “manage.py test” — authentication and redirects are working for User1 and User2 successfully, however it doesn’t authenticate for User3 and returns the “fail2.html” template. All other tests with User3 have returned valid results.
Any suggestions? This is my first question post, so feel free to ask questions if I’ve left relevant information out!
Thanks in advance.
Not really an answer for your question, but why not use a user profile to determine your
typeand other data specific to theUserXclasses? They are easy to set up, allow you to store additional User information, and allows you to continue using the built inUserobjects.The profile classes are pretty easy (I lifted this from The Django Book, Chapter 12):
Beyond that, you change on item in
settings.py, and set up a trigger to automagically create the profile onUsercreation, and you’re good to go.Your resulting view code would be drastically simplified, too: