本文整理汇总了Python中django.contrib.auth.forms.AuthenticationForm方法的典型用法代码示例。如果您正苦于以下问题:Python forms.AuthenticationForm方法的具体用法?Python forms.AuthenticationForm怎么用?Python forms.AuthenticationForm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.auth.forms
的用法示例。
在下文中一共展示了forms.AuthenticationForm方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: login_view
# 需要导入模块: from django.contrib.auth import forms [as 别名]
# 或者: from django.contrib.auth.forms import AuthenticationForm [as 别名]
def login_view(request):
if request.user.is_authenticated:
return HttpResponseRedirect('/post/')
if request.method == 'GET':
form = AuthenticationForm()
return render(request, 'tilweb/login.html', {'form': form})
if request.method == 'POST':
form = AuthenticationForm(request=request, data=request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username, password=password)
if user is not None:
print(user)
login(request, user)
return HttpResponseRedirect('/post/')
else:
print('User not found')
else:
# If there were errors, we render the form with these
# errors
return render(request, 'tilweb/login.html', {'form': form})
示例2: user_login
# 需要导入模块: from django.contrib.auth import forms [as 别名]
# 或者: from django.contrib.auth.forms import AuthenticationForm [as 别名]
def user_login(request):
"""
View for logging users in.
"""
redirect_to = request.POST.get(REDIRECT_FIELD_NAME, request.GET.get(REDIRECT_FIELD_NAME, ''))
login_form = AuthenticationForm(request, data=request.POST)
if login_form.is_valid():
# Ensure the user-originating redirection url is safe.
if not is_safe_url(url=REDIRECT_FIELD_NAME, host=request.get_host()):
redirect_to = settings.LOGIN_REDIRECT_URL
# Okay, security check complete. Log the user in.
auth_login(request, login_form.get_user())
return redirect(settings.LOGIN_REDIRECT_URL if redirect_to == '' else redirect_to)
else:
return render(request, 'index.html', {'login_form': login_form, 'display': 'block', 'active': 'login'})
示例3: login_view
# 需要导入模块: from django.contrib.auth import forms [as 别名]
# 或者: from django.contrib.auth.forms import AuthenticationForm [as 别名]
def login_view(request):
if request.user.is_authenticated:
return close_response(request)
if request.method == 'POST':
form = AuthenticationForm(request, data=request.POST)
if form.is_valid():
login(request, form.user_cache)
redeem_token_after_login(request)
return close_response(request)
else:
form = AuthenticationForm(request)
ctx = {
'title': _('Log in'),
'form': form,
}
if settings.USER_REGISTRATION:
ctx.update({
'bottom_link_url': reverse('site.register'),
'bottom_link_text': _('Create new account')
})
return render(request, 'site/account_form.html', ctx)
示例4: login_view
# 需要导入模块: from django.contrib.auth import forms [as 别名]
# 或者: from django.contrib.auth.forms import AuthenticationForm [as 别名]
def login_view(request):
redirect_path = request.GET['r'] if request.GET.get('r', '').startswith('/editor/') else reverse('editor.index')
if request.user.is_authenticated:
return redirect(redirect_path)
if request.method == 'POST':
form = AuthenticationForm(request, data=request.POST)
if form.is_valid():
login(request, form.user_cache)
return redirect(redirect_path)
else:
form = AuthenticationForm(request)
return render(request, 'editor/account_form.html', {
'title': _('Log in'),
'form': form,
'bottom_link_url': reverse('site.register'),
'bottom_link_text': _('Create new account')
})
示例5: login
# 需要导入模块: from django.contrib.auth import forms [as 别名]
# 或者: from django.contrib.auth.forms import AuthenticationForm [as 别名]
def login(request):
"""
Log a user in.
"""
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
login_user(request, form.get_user())
return redirect(request.GET['next'] if 'next' in request.GET else 'home')
else:
form = AuthenticationForm()
return render(request, 'accounts/login.html', {
'title': 'Login',
'form': form,
'action': 'Login',
})
示例6: login_request
# 需要导入模块: from django.contrib.auth import forms [as 别名]
# 或者: from django.contrib.auth.forms import AuthenticationForm [as 别名]
def login_request(request):
if request.method == "POST":
form = AuthenticationForm(request, data=request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username,password=password)
if user is not None:
login(request,user)
messages.info(request, _("You are now logged in as ") + username)
return redirect("main:homepage")
else:
messages.error(request, _("Invalid ysername or password or recaptcha"))
else:
messages.error(request, _("Invalid ysername or password or recaptcha"))
form = AuthenticationForm()
return render(request,
"main/login.html",
{"form":form})
示例7: create
# 需要导入模块: from django.contrib.auth import forms [as 别名]
# 或者: from django.contrib.auth.forms import AuthenticationForm [as 别名]
def create(self, request):
"""
Log in django staff user
"""
# TODO: Decorate api with sensitive post parameters as Django admin do?
# from django.utils.decorators import method_decorator
# from django.views.decorators.debug import sensitive_post_parameters
# sensitive_post_parameters_m = method_decorator(sensitive_post_parameters())
login_form = AuthenticationForm(request, data=request.data)
if not login_form.is_valid():
raise serializers.ValidationError(login_form.errors)
auth_login(request, login_form.get_user())
serializer = UserSerializer(request.user)
return Response(serializer.data, status=status.HTTP_200_OK)
示例8: login
# 需要导入模块: from django.contrib.auth import forms [as 别名]
# 或者: from django.contrib.auth.forms import AuthenticationForm [as 别名]
def login(request):
if request.user.is_authenticated:
if 'next' in request.GET:
return HttpResponseRedirect(request.GET['next'])
else:
return HttpResponseRedirect('/')
if request.method == 'POST':
form = AuthenticationForm(request.POST)
user = authenticate(request, username=request.POST['username'], password=request.POST['password'])
if user:
auth_login(request, user)
return HttpResponseRedirect('/')
else:
messages.add_message(request, messages.ERROR, _(u'Data invalid'))
form = AuthenticationForm()
if hasattr(settings, 'SSO_PRIVATE_KEY') and settings.SSO_PRIVATE_KEY:
sso = True
else:
sso = False
return render(request, 'login.html', {'form': form, 'sso': sso})
示例9: home
# 需要导入模块: from django.contrib.auth import forms [as 别名]
# 或者: from django.contrib.auth.forms import AuthenticationForm [as 别名]
def home(request):
if request.user.is_authenticated:
return HttpResponseRedirect(reverse('projects'))
else:
if settings.SHIBBOLETH:
return render(request, 'core/home.html')
elif settings.ACCOUNT or settings.SOCIALACCOUNT:
from allauth.account.forms import LoginForm
return render(request, 'core/home.html', {'form': LoginForm()})
else:
from django.contrib.auth.forms import AuthenticationForm
return render(request, 'core/home.html', {'form': AuthenticationForm()})
示例10: login
# 需要导入模块: from django.contrib.auth import forms [as 别名]
# 或者: from django.contrib.auth.forms import AuthenticationForm [as 别名]
def login(request):
if request.user.is_authenticated:
messages.add_message(
request, messages.INFO, message=_("You are already logged in.")
)
return redirect("home")
context = {"login_form": AuthenticationForm(), "signup_form": UserCreationForm()}
if request.method == "POST":
if request.POST.get("action") == "login":
login_form = AuthenticationForm(data=request.POST)
if login_form.is_valid():
username = login_form.cleaned_data.get("username")
raw_password = login_form.cleaned_data.get("password")
user = authenticate(username=username, password=raw_password)
login_user(request, user, backend=user.backend)
return redirect("home")
else:
context["login_form"] = login_form
context["action"] = "login"
elif request.POST.get("action") == "signup":
signup_form = UserCreationForm(data=request.POST)
if signup_form.is_valid():
signup_form.save()
username = signup_form.cleaned_data.get("username")
raw_password = signup_form.cleaned_data.get("password1")
user = authenticate(username=username, password=raw_password)
login_user(request, user, backend=user.backend)
return redirect("home")
else:
context["signup_form"] = signup_form
context["action"] = "signup"
return render(request, "get_together/users/login.html", context)
示例11: login
# 需要导入模块: from django.contrib.auth import forms [as 别名]
# 或者: from django.contrib.auth.forms import AuthenticationForm [as 别名]
def login(self, request, *args, **kwargs):
# django-rest-framework doesn't do this for logged out requests
SessionAuthentication().enforce_csrf(request)
if request.user.is_authenticated:
raise ParseError(_('Log out first.'))
data = get_api_post_data(request)
if 'token' in data:
try:
token = Token.get_by_token(data['token'])
except Token.DoesNotExist:
raise PermissionDenied(_('This token does not exist or is no longer valid.'))
user = token.user
elif 'username' in data:
form = AuthenticationForm(request, data=data)
if not form.is_valid():
raise ParseError(form.errors)
user = form.user_cache
else:
raise ParseError(_('You need to send a token or username and password.'))
login(request, user)
return Response({
'detail': _('Login successful.'),
'csrf_token': csrf.get_token(request),
})
示例12: get_token
# 需要导入模块: from django.contrib.auth import forms [as 别名]
# 或者: from django.contrib.auth.forms import AuthenticationForm [as 别名]
def get_token(self, request, *args, **kwargs):
# django-rest-framework doesn't do this for logged out requests
SessionAuthentication().enforce_csrf(request)
data = get_api_post_data(request)
form = AuthenticationForm(request, data=data)
if not form.is_valid():
raise ParseError(form.errors)
token = form.user_cache.login_tokens.create()
return Response({
'token': token.get_token(),
})
示例13: test_displays_login_form
# 需要导入模块: from django.contrib.auth import forms [as 别名]
# 或者: from django.contrib.auth.forms import AuthenticationForm [as 别名]
def test_displays_login_form(self):
response = self.client.get(self.login_url)
self.assertIsInstance(response.context["form"], AuthenticationForm)
示例14: test_disable_signups_has_special_instructions
# 需要导入模块: from django.contrib.auth import forms [as 别名]
# 或者: from django.contrib.auth.forms import AuthenticationForm [as 别名]
def test_disable_signups_has_special_instructions(self):
with TempSiteID(2):
Site.objects.create(id=2)
response = self.client.get(self.login_url)
self.assertIsInstance(response.context["form"], AuthenticationForm)
self.assertContains(response, "You should have gotten an email")
示例15: log_in
# 需要导入模块: from django.contrib.auth import forms [as 别名]
# 或者: from django.contrib.auth.forms import AuthenticationForm [as 别名]
def log_in(request):
form = AuthenticationForm()
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
login(request, form.get_user())
return redirect(reverse('example:user_list'))
else:
print(form.errors)
return render(request, 'example/log_in.html', {'form': form})