Reputation: 319
I can't expose Email field from User model in django, If I remove email field from register.html there is no error. Also if I use {{ form }} It does not show the email field. It shows username, password1 and password2 with no error.
models.py:
from django.contrib.auth.models import User
class Profile(models.Model):
profile_pic = models.ImageField(null=True, blank=True, default='default.jpg')
# FK
user = models.ForeignKey(User, max_length=10, on_delete=models.CASCADE, null=True)
forms.py:
from django.contrib.auth.models import User
class CreateUserForm(UserCreationForm):
class Meta:
model = User
fields= ['username','email', 'password1', 'password2']
views.py: from .forms import UserCreationForm
def register(request):
form = UserCreationForm()
if request.method == "POST":
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return redirect("my-login")
context = {'form': form}
return render(request, "register.html", context=context)
register.html
{% load static %}
{% load crispy_forms_tags %}
.
.
.
<form action="" method="POST" autocomplete="off">
{% csrf_token %}
{{ form.username|as_crispy_field }}
# error is here
{{ form.email|as_crispy_field }}
{{ form.password1|as_crispy_field }}
{{ form.password2|as_crispy_field }}
<input type="submit" value="submit" />
</form>
Django 4.2.10
django-bootstrap4 24.1
django-crispy-forms 1.14.0
pillow 10.2.0
wheel 0.42.0
Upvotes: 0
Views: 54
Reputation: 319
Solution: It was my fault in importing.
in Views.py I should import
from . forms import CreateUserForm
but I have imported:
from .forms import UserCreationForm
Upvotes: 0