0

I am trying to build a login module in django. I already have a nice HTML form for login which has username and password field.

But in my views.py I have imported default Django AuthenticationForm, but if integrate it with my nice-looking HTML form, there will be two forms. How can I import the Authentication form and use it with the my HTML form??

My code is here:

My view:

def login(request):
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            user = form.get_user()
            authlog(request, user)
            return redirect('home')
        else:
            messages.error(request, 'Invalid username or password')
            # back_page = request.META.get('HTTP_REFERER')
            return redirect('login')
            
            # return HttpResponse(back_page)
    else:
        content = {
            'form': AuthenticationForm()
        }
        return render(request, 'sign-in.html', content)

If I use Django default form, the code will be following, but it won't look good as my other Html login page.

My sign-in html:

<hr>
<form action="" method="post">
    {% csrf_token %}
    {{form|crispy}}
    <button type="submit" class=" btn btn-success ">Sign-Up</button>

</form>

I haven't posted my other HTML form though as it is very large. Hope it explains.

3
  • What is the purpose of your custom form? The default Django form, has all the fields you need in order to login. You can use that form and style it as you like in your template. Commented Dec 11, 2020 at 7:58
  • My custom fields have all the styles and images and svgs needed. And it has all the classes, but I how can I style my default form like that {{form|crispy}}?? Cant I just put {{form|crispy}} inside the custom form?? Commented Dec 11, 2020 at 8:03
  • you can add css class to your form within widget, take a look at stackoverflow.com/a/50524134/11225821 Commented Dec 11, 2020 at 11:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.