當前位置: 首頁>>代碼示例>>Python>>正文


Python forms.AuthenticationForm方法代碼示例

本文整理匯總了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}) 
開發者ID:amitsaha,項目名稱:python-monitoring-talk,代碼行數:24,代碼來源:views.py

示例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'}) 
開發者ID:Djacket,項目名稱:djacket,代碼行數:18,代碼來源:views.py

示例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) 
開發者ID:c3nav,項目名稱:c3nav,代碼行數:27,代碼來源:views.py

示例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')
    }) 
開發者ID:c3nav,項目名稱:c3nav,代碼行數:21,代碼來源:account.py

示例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',
    }) 
開發者ID:2buntu,項目名稱:2buntu-blog,代碼行數:18,代碼來源:views.py

示例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}) 
開發者ID:kiahamedi,項目名稱:medadfeshari,代碼行數:24,代碼來源:views.py

示例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) 
開發者ID:5monkeys,項目名稱:django-bananas,代碼行數:20,代碼來源:views.py

示例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}) 
開發者ID:mediafactory,項目名稱:yats,代碼行數:23,代碼來源:views.py

示例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()}) 
開發者ID:rdmorganiser,項目名稱:rdmo,代碼行數:14,代碼來源:views.py

示例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) 
開發者ID:GetTogetherComm,項目名稱:GetTogether,代碼行數:36,代碼來源:user.py

示例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),
        }) 
開發者ID:c3nav,項目名稱:c3nav,代碼行數:31,代碼來源:api.py

示例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(),
        }) 
開發者ID:c3nav,項目名稱:c3nav,代碼行數:17,代碼來源:api.py

示例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) 
開發者ID:project-callisto,項目名稱:callisto-core,代碼行數:5,代碼來源:test_views.py

示例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") 
開發者ID:project-callisto,項目名稱:callisto-core,代碼行數:8,代碼來源:test_views.py

示例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}) 
開發者ID:realpython,項目名稱:django-example-channels,代碼行數:12,代碼來源:views.py


注:本文中的django.contrib.auth.forms.AuthenticationForm方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。