显示没有输入字段的Django表单 [英] Django Form Showing No Input Fields

查看:121
本文介绍了显示没有输入字段的Django表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我请求我的表单时,我得到了一个有效的回复,但是我没有得到响应的表单域。它正在加载提交按钮,但没有表单字段。



目标:获取表单字段加载并能够提交表单。 / p>

我有一个 views.py

  def注册(请求):
form = NewUserRegistrationForm(request.POST或None)
如果request.method =='POST':
if form.is_valid():
form.save()
return HttpResponseRedirect(/ Login /)
else:
form = NewUserRegistrationForm()

返回渲染(请求,重复使用/ register.html',{
'form':form
})

forms.py

  class NewUserRegistrationForm(UserCreationForm):
username = forms。 CharField(required = True,max_length = 30,validators = [RegexValidator('^ [A-Za-z0-9] {1,30} $','eg must be 30 characters or less','Invalid Entry' )
email = forms.EmailField(r equired = True,max_length = 75)
password = forms.PasswordInput()
class Meta:
model =用户
fields =(username,email,password1 ,password2)

def save(self,commit = True):
user = super(NewUserRegistrationForm,self).save(commit = False)
user.username = self.cleaned_data [username]
user.email = self.cleaned_data [email]
user.password = self.cleaned_data [password1]
如果提交:
user.save()
返回用户

a 模板

 < div id =register_bubble> 
< form method =postid =userRegistration>
{%csrf_token%}
{{NewUserRegForm.as_p}}
< input type =submitvalue =Submit/>
< / form> <! - / RegistrationForm(FORM) - >
< / div>

我在这里做错什么?



谢谢!

解决方案

您有两个错误。



首先,您将表单类传递到模板上下文中,而不是表单实例:该类是 NewUserRegistrationForm ,但是您已将其实例化为 NewUserRegForm ,这就是您应该在表单上下文中作为值传递的内容。



为了使其更复杂,您给予该值的键名也是 NewUserRegistrationForm - 但是'仍然指向 NewUserRegForm 在模板中,即使这不存在。



这将是如果您使用符合PEP8标准的名称,则更为明显。实例应为小写,带下划线:例如 new_user_registration_form 。但是,在这种情况下,您只能将其称为表单,因为只有一个。

  return render(request,'mysite / reuse / register.html',{
'NewUserRegForm':NewUserRegForm
})

或者更好:

  form = NewUserRegistrationForm(request.POST或没有)
...
return render(request,'mysite / reuse / register.html',{
'form':form
})


I am getting a valid response back when requesting my form, but I am getting no form fields with the response. It is loading the Submit button only, but no form fields.

Goal: get form fields to load and be able to submit form.

I have a views.py:

 def Registration(request):
    form = NewUserRegistrationForm(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            return HttpResponseRedirect("/Login/")
    else:
        form = NewUserRegistrationForm()

    return render(request, 'VA/reuse/register.html', {
        'form': form 
    })

forms.py

class NewUserRegistrationForm(UserCreationForm):
    username = forms.CharField(required=True,max_length=30,validators=[RegexValidator('^[A-Za-z0-9]{1,30}$','e.g. must be 30 characters or less','Invalid Entry')])
    email = forms.EmailField(required=True, max_length=75)
    password = forms.PasswordInput()
    class Meta:
        model = User
        fields = ("username", "email", "password1","password2")

def save(self, commit=True):
    user = super(NewUserRegistrationForm, self).save(commit=False)
    user.username = self.cleaned_data["username"]
    user.email = self.cleaned_data["email"]
    user.password = self.cleaned_data["password1"]
    if commit:
        user.save()
    return user

a template

<div id="register_bubble">
    <form method="post" id="userRegistration">
        {% csrf_token %}
        {{ NewUserRegForm.as_p }}
        <input type="submit" value="Submit" />
    </form> <!-- /RegistrationForm (FORM) -->
</div>

What am I doing wrong here? I'm getting no error while in debug mode locally either.

Thank you!

解决方案

You have two mistakes.

Firstly, you're passing the form class into the template context, not the form instance: the class is NewUserRegistrationForm, but you've instantiated it as NewUserRegForm, and that's what you should be passing as the value in the form context.

To make it more complicated, the key name you've given to that value is also NewUserRegistrationForm - but you're still referring to NewUserRegForm in the template, even though that doesn't exist there.

This would be much more obvious if you used PEP8 compliant names. Instances should be lower case with underscore: eg new_user_registration_form. However, in this case you could just call it form, since there's only one.

return render(request, 'mysite/reuse/register.html', {
    'NewUserRegForm': NewUserRegForm 
})

or, better:

form = NewUserRegistrationForm(request.POST or None)
...
return render(request, 'mysite/reuse/register.html', {
    'form': form 
})

这篇关于显示没有输入字段的Django表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆