开发者

django login authentication

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:
 开发者_Go百科           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 type and other data specific to the UserX classes? They are easy to set up, allow you to store additional User information, and allows you to continue using the built in User objects.

The profile classes are pretty easy (I lifted this from The Django Book, Chapter 12):

class MySiteProfile(models.Model):
    # This is the only required field
    user = models.ForeignKey(User, unique=True)

    # The rest is completely up to you...
    type = models.CharField(maxlength=100, blank=True)

Beyond that, you change on item in settings.py, and set up a trigger to automagically create the profile on User creation, and you're good to go.

Your resulting view code would be drastically simplified, too:

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)
            type = user.get_profile().type

            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)
            )


This is not exactly an answer to your problem but have you considered using groups/permissions to differentiate between different types of users rather than creating subclasses of auth.User? Django's auth feature comes with a reasonably useful groups/permissions mechanism which can be leveraged to make your life more easier.

Using groups you can various create groups ("undergrad", "grad" etc.) and grant each group appropriate permissions to achieve the necessary effect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜