本文整理汇总了Python中forms.LoginForm.get_username方法的典型用法代码示例。如果您正苦于以下问题:Python LoginForm.get_username方法的具体用法?Python LoginForm.get_username怎么用?Python LoginForm.get_username使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类forms.LoginForm
的用法示例。
在下文中一共展示了LoginForm.get_username方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: login
# 需要导入模块: from forms import LoginForm [as 别名]
# 或者: from forms.LoginForm import get_username [as 别名]
def login(request):
"""
A view for logging in users
"""
# forward users who are already logged in to their homepage
if request.user.is_authenticated():
msg = 'You are already logged in and are forwarded to your Inbox'
messages.info(request, msg)
return HttpResponseRedirect(reverse('url_index'))
next = None
invalid = False
if request.method == 'POST':
# propagate 'next'
if 'next' in request.POST:
next = request.POST['next']
form = LoginForm(request.POST)
if form.is_valid():
# get the username
# (if there is no username, the form is not valid)
username = form.get_username()
password = form.cleaned_data['password']
# authenticate
user = auth.authenticate(username=username, password=password)
# (if the User object is not active, the form is not valid)
if user is not None:
# login
auth.login(request, user)
# check redirect
if 'next' in request.POST:
next = request.POST['next']
return HttpResponseRedirect(next)
# re-direct to home page
return HttpResponseRedirect(reverse('url_index'))
else:
invalid = True
else:
# create an empty form
form = LoginForm()
# propagate 'next'
if request.method == 'GET' and 'next' in request.GET:
next = request.GET['next']
return render_to_response('registration/login.html',
{ 'form' : form,
'next' : next,
'invalid' : invalid },
context_instance=RequestContext(request))
示例2: login
# 需要导入模块: from forms import LoginForm [as 别名]
# 或者: from forms.LoginForm import get_username [as 别名]
def login(request):
login_form = LoginForm()
next = request.REQUEST.get('next')
if request.method == 'POST':
login_form = LoginForm(request.POST)
if login_form.is_valid():
username = login_form.get_username()
request.session['username'] = username
if next:
return HttpResponseRedirect(next)
return HttpResponseRedirect('/')
context = {
'login_form': login_form,
'next': next,
}
return render_to_response('login.html', context,
context_instance=RequestContext(request))