I've set up user account signup and login pages following this tutorial and it all works great, except the pages have no formatting. I'm now looking for a simple drop in solution to improve the appearance of "templates/registration/login.html" and "templates/registration/signup.html". Someone recommended crispy forms which look great, and as the rest of my site uses Bootstrap 5, crispy-bootstrap5 looks ideal.
I'm struggling to implement crispy-bootstrap5 as I don't understand Django's inbuilt django.contrib.auth.forms
nor forms in general, and can't find simple reproducible examples for crispy forms with signup.html and login.html. I've installed packages fine, but now don't know how to beautify login.html and signup.html from that tutorial:
{% extends 'base.html' %}
{% block title %}Sign Up{% endblock %}
{% block content %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Sign Up</button>
</form>
{% endblock %}
I don't know where to go with this, but the docs for regular django-crispy links to a gist for an eg form, and the index.html {% load crispy_forms_tags %}
and {% crispy form %}
. I've put them in which mostly left the page unchanged except for messing up the formatting. I think I now need to modify class CustomUserCreationForm(UserCreationForm)
in forms.py but I don't know how. I'm also reluctant to risk bigger problems by modifying Django's inbuilt functionalities I don't understand.
What steps need we take to get login.html and signup.html looking like bootstrap 5?
please follow bellow steps to use crispy_forms with bootstrap5:
1st step: Install crispy_forms
form bootstrap 5
pip install crispy-bootstrap5
step 2: Add crispy_forms
and crispy_bootstrap5
to installed apps
INSTALLED_APPS = (
...
"crispy_forms",
"crispy_bootstrap5",
...
)
step 3: add crispy form attr to settings
file
CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
CRISPY_TEMPLATE_PACK = "bootstrap5"
check here for details
to use crispy form in templates, add {% load crispy_forms_filters %}
and {% load crispy_forms_tags %}
on top of your sign up
and login
page.
and declare form as crispy i.e {{forms| crispy}}
or field as {{ form.fieldname|as_crispy_field }}