def index(request):
    """
    This view will handle the signin process
    """
    if not request.user.is_authenticated:
        if request.method == "POST":
            form = AuthenticationForm(request=request, data=request.POST)
            if form.is_valid():
                requested_username = form.cleaned_data["username"]
                requested_password = form.cleaned_data["password"]
                user = authenticate(username=requested_username,
                                    password=requested_password)
                if user is not None:
                    perform_login(request, user)
                    messages.success(request, "Logged in Successfully")
                    return HttpResponseRedirect("/dashboard/")
                else:
                    messages.error(request,
                                   "Please enter valid username and password")
                    return HttpResponseRedirect(reverse("user:index"))
            else:
                messages.error(request, "Please enter valid data")
                return HttpResponseRedirect(reverse("user:index"))
        else:
            context = {'form': AuthenticationForm()}
            return render(request, "user/index.html", context)
    else:
        messages.success(request, "Already Logged In")
        return render(request, "user/dashboard.html", {})
示例#2
0
文件: views.py 项目: antiface/ductus
def user_creation(request, template_name='registration/create_user.html'):
    "Displays user creation form and handles its action"
    if request.method == "POST":
        if recaptcha is not None:
            if not ('recaptcha_challenge_field' in request.POST
                    and 'recaptcha_response_field' in request.POST
                    and recaptcha.submit(request.POST['recaptcha_challenge_field'],
                                         request.POST['recaptcha_response_field'],
                                         settings.RECAPTCHA_PRIVATE_KEY,
                                         request.remote_addr).is_valid):
                from django.http import HttpResponseForbidden
                return HttpResponseForbidden("invalid captcha")

        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            # log in the user automatically
            user = authenticate(username=form.cleaned_data["username"],
                                password=form.cleaned_data["password1"])
            perform_login(request, user)
            return redirect("ductus.wiki.views.view_frontpage")
    else:
        form = UserCreationForm()

    captcha_html = ""
    if recaptcha is not None:
        captcha_html = recaptcha.displayhtml(settings.RECAPTCHA_PUBLIC_KEY,
                                             use_ssl=request.is_secure())
        captcha_html = captcha_html.replace('frameborder="0"',
                                            'style="border: 0"')

    return render_to_response(template_name, {
        'form': form,
        'captcha': mark_safe(captcha_html),
    }, context_instance=RequestContext(request))
示例#3
0
def register(request):
    form = UserCreationForm(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            new_user = authenticate(
                username=form['username'].value(),
                password=form['password1'].value(),
            )
            perform_login(request, new_user)
            return redirect('load')
    return render(request, 'users/register.jinja', {
        'form': form,
    })