我觉得@login_required
非常有用。我@login_required(login_url='/login/')
在每个视图之前都使用过这个。如果用户尝试访问/upload
url,它会重定向到 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))