7👍
✅
You are importing the wrong UserCreationForm
in views.py. You should import your own form not the Django’s one:
stories/views.py
from stories.forms import UserCreationForm
...
Besides that, you don’t have to wrap all your fields with <p></p>
individually as there exists form.as_p()
for this job.
register.html
<form action = "/register/" method = "POST">{% csrf_token %}
{{ form.as_p }}
</form>
Hope this helps.
5👍
I’m new with django and I tried what you posted and I had to changed to work … Here’s what I did.
forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class UserCreationForm(UserCreationForm):
email = forms.EmailField(required=True, label='Email')
class Meta:
model = User
fields = ("username", "email", "password1", "password2")
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.email = self.cleaned_data["email"]
if commit:
user.save()
return user
views.py
from .forms import UserCreationForm
from django.urls import reverse_lazy
from django.views import generic
class SignUp(generic.CreateView):
form_class = UserCreationForm
success_url = reverse_lazy('login')
template_name = 'accounts/signup.html'
signup.html
{% extends 'polls/base.html' %}
{% load bootstrap4 %}
{% load static %}
{% block content %}
<body class="body_login">
<form method="post" class="form-signup">
{% csrf_token %}
{% bootstrap_form form %}
<button type="submit" class="save btn btn-dark">Sign up</button>
</form>
</body>
{% endblock %}
- Django id integer limit
- How do I create a wheel from a django app?
- Django-compressor: how to write to S3, read from CloudFront?
0👍
forms.py
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class SignupForm(UserCreationForm):
class Meta:
model = User
fields = ("username", "email",)
views.py
from django.urls import reverse_lazy
from django.views import generic
from accounts.forms import SignupForm
class SignUpView(generic.CreateView):
form_class = SignupForm
success_url = reverse_lazy('login')
template_name = 'stories/register.html'
👤Điệp
- Python from django.contrib.auth.views import logout ImportError: cannot import name 'logout'
- No connection could be made because the target machine actively refused it (Django)
- How do I call a model method in django ModelAdmin fieldsets?
- Django or web.py, which is better to build a large website with Python?
- Django REST Framework Swagger 2.0
Source:stackexchange.com