Try with Class based views:
form:
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class CadastroForm(UserCreationForm):
class Meta:
model = User
fields = ['username', 'password', 'first_name', 'last_name', 'email']
In the list Fields, are the default names of the Django user, this reusing the.
In your case it would only be email and password but here is the reference.
View:
from django.views.generic import CreateView
from django.urls import reverse_lazy
from .forms import CadastroForm
class Usuario(CreateView):
form_class = CadastroForm
template_name = 'cadastro.html'
# success_url = reverse_lazy('sucesso') #redireciona para algum lugar
# ou as mensagens personalidadas do cbv
# Pode dar erro indicando falta de redirecionamento se não tratar.
urls:
from .views import Usuario
urlpatterns = [
path('cadastrar/', Usuario.as_view())
html:
<form method="post">
{%csrf_token%}
{{ form.as_p }}
<button type="submit">salvar</button>
</form>
Confirm by looking in the database if the data has been saved.
Still not saving...
– Bruno Henrique