我设法让股票标准用户创建表格起作用。其中只包括用户名,密码1和密码2字段。但是,当我尝试包含电子邮件字段时,它永远不会显示在我的模板中。我想我也许在想法中遗漏了一些东西。这是我的代码:
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)
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 django.contrib.auth.forms import UserCreationForm
def register_user(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
args = {}
args.update(csrf(request))
args['form'] = UserCreationForm()
return render_to_response('stories/register.html', args)
register.html
<form action = "/register/" method = "POST">
{% csrf_token %}
<p>
{{ form.username.label_tag }}
{{ form.username}}
</p>
<p>
{{ form.email.label_tag }}
{{ form.email }}
</p>
<p>
{{ form.password1.label_tag }}
{{ form.password1 }}
</p>
<p>
{{ form.password2.label_tag }}
{{ form.password2 }}
</p>
<input type = "submit" value = "register" />
</form>
此文件中的所有字段都将呈现在视图中, 电子邮件字段除外。
任何人都能发现原因吗?!
答案 0 :(得分:3)
您在views.py中导入了错误的JAVA_HOME
。您应该导入自己的表单而不是Django的表单:
<强>故事/ views.py 强>
UserCreationForm
除此之外,您不必单独使用from stories.forms import UserCreationForm
...
包装所有字段,因为此作业存在form.as_p()
。
<强> register.html 强>
<p></p>
希望这有帮助。
答案 1 :(得分:1)
我是django的新手,我尝试了您发布的内容,我不得不更改为工作……这就是我所做的。
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 %}
答案 2 :(得分: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'