Django Form Showing No Input Fields

僤鯓⒐⒋嵵緔 提交于 2019-12-24 13:45:01

问题


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!


回答1:


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 
})



回答2:


You're passing the form instance to the context as 'form', but calling it in the template as {{ NewUserRegForm.as_p }}.

You should use {{ form.as_p }} instead.



来源:https://stackoverflow.com/questions/18806668/django-form-showing-no-input-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!