George Vas
George Vas

Reputation: 125

Django user passing

I am a newbie to python and django and I have to build a web app. It's supposed that after a user logs in he is redirected to another page where he chooses a project to work on. The problem is that AFTER successfully logging in when he chooses a project I get the "Invalid login credentials" error from the login view. I thought we were past this... Here is the views file:


def login(request):
    context=RequestContext(request)
    if request.method=='POST':
        username=request.POST.get('username')
        password=request.POST.get('password')
        user=auth.authenticate(username=username, password=password)
        if user:
            if user.is_active:
                auth.login(request,user)
                return render_to_response('ProjectLogging/main.html',{'user':user, 'project_list':Project.objects.all()}, context)
            else:
                return HttpResponse("Your account is disabled.")
        else:
            return HttpResponse("Invalid login credentials.")
    return render_to_response('ProjectLogging/login.html',context)

@login_required
def logout(request, user):
    context=RequestContext(request)
    auth.logout(request, user)
    return render_to_response('ProjectLogging/login.html',{'user':None},context)

@login_required
def main(request, user):
    context = RequestContext(request)
    user=user
    if request.method=='POST':
        project=request.POST['project']
        if project:
            change=Change(user=user, project=project,starttime=datetime.datetime.now())
            change.save()
        else:
            HttpResponse("Choose a valid project!")
    else:
        HttpResponse("Choose a POST method (???????)")

This is the urls of the project:

urlpatterns=patterns('',
    url(r'', 'views.login'),
    url(r'^login/$', 'views.login'),
    url(r'^logout/$', 'views.logout'),
    url(r'^main/$','views.main'),

)

Also, I haven't mastered how views, urls and html files collaborate in django, so if you can suggest a site to study that I would be grateful. Finally, logout isn't working so if you have any idea about that I would be even more grateful. Thank you.

Upvotes: 1

Views: 60

Answers (1)

knbk
knbk

Reputation: 53699

Url patterns use regular expressions and return the first match. In a regular expression, a ^ signifies the start of a string, and a $ the end. If neither is present, the pattern can be found anywhere in the string and it will still match. Now take a look at your first pattern.

    url(r'', 'views.login'),

This pattern can match anywhere in the string, and it matches if the string "contains" an empty string, i.e. every single string you can think of. All requests will be directed to your views.login view.

To fix it, you must use the pattern r'^$'. This will match the start and the end of the url path with nothing in between, i.e. only www.example.com/ (the first slash is always chopped of). Then you can reach your other views at /logout/ and /main/.

Upvotes: 2

Related Questions