0

我觉得@login_required非常有用。我@login_required(login_url='/login/')在每个视图之前都使用过这个。如果用户尝试访问/uploadurl,它会重定向到 url:http://127.0.0.1:8000/login/?next=/upload_file/我的 url 非常简单:

urlpatterns = patterns('',

    #url(r'^admin/', include(admin.site.urls)),
    url(r'^upload_file/', 'fileupload.views.upload_file'),
    url(r'^show_file/', 'fileupload.views.show_list'),
    url(r'^sync/', 'fileupload.views.sync'),
    url(r'^login/', 'fileupload.views.login_user'),
    url(r'^upload_file_form', 'fileupload.views.upload_file_form' )
)

现在,当用户按下登录时,页面被重定向到无处。我确定我的网址格式不正确。我错过了什么?谢谢

编辑

def login_user(request):
    errors = []
    state = "LOG IN"
    username = password = ''
    if request.method == 'POST':
        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)
                state = "You're successfully logged in!"
            else:
                state = "Your account is not active, please contact the site admin."
        else:
            state = "Your username and/or password were incorrect."

    return render_to_response('login.html', {'state':state, 'username': username}, context_instance=RequestContext(request))
4

1 回答 1

2

您的fileupload.views.login_user视图需要尊重next查询字符串参数本身。这是 Django 登录视图的内置功能,但由于您已经推出了自己的功能,因此您需要自己实现该功能。

于 2012-12-26T10:24:14.190 回答