简体   繁体   English

表单字段不显示在django模板中,只显示提交按钮

[英]Form fields are not shown in django templates, only submit button

Hello everybody!!! 大家好!!! I have a problem with forms, below is the code, any help will be appreciated... 我有表格问题,下面是代码,任何帮助将不胜感激...

views.py views.py

def my_registration_view(request):
    if request.method == 'POST':
        form = MyForm(request.POST)
    if form.is_valid():
        user = User.objects.create_user(username=form.cleaned_data['username'],
                                        first_name=form.cleaned_data['first_name'],
                                        last_name=form.cleaned_data['last_name'],
                                        email=form.cleaned_data['email'],
                                        password=form.cleaned_data['password'],)
        user.save()
        return HttpResponseRedirect(reverse('profile', args=[user.id]))

    else:
        return render_to_response('register.html', {'form': form}, context_instance=RequestContext(request))

    else:
        form = MyForm()
        context = {'form': form}
        return render_to_response('register.html', context, context_instance=RequestContext(request))

my forms.py which works fine 我的forms.py工作正常

class MyForm(forms.Form):
    username = forms.CharField(max_length=25)
    first_name = forms.CharField(max_length=25)
    last_name = forms.CharField(max_length=25)
    email = forms.EmailField()
    password = forms.CharField(widget=forms.PasswordInput())

and here is my forms.py when it is replaced, stopped showing the fields. 这是我的forms.py,当它被替换,停止显示字段。

class MyForm(forms.Form):
    class Meta:
    model = User
    fields = ['username', 'first_name', 'last_name', 'email', 'password']

here is the register.html 这是register.html

<form action="{% url 'register' %}" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type='submit' value='Submit' />
</form>

Replace forms.Form with forms.ModelForm. 用forms.ModelForm替换forms.Form。 This should work. 这应该工作。

class MyForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ['username', 'first_name', 'last_name', 'email', 'password']

In you example you have not mentioned forms.typeField , Look in the below example: 在您的示例中,您没有提到forms.typeField ,请查看以下示例:

For you Html page some thing like this: 为你的Html页面这样的事情:

<form action="/your-name/" method="post">
<label for="your_name">Your name: </label>
<input id="your_name" type="text" name="your_name" value="{{ current_name }}">
<input type="submit" value="OK">
</form>

In forms.py you should have fields 在forms.py中,您应该有字段

form.py
from django import forms

class NameForm(forms.Form):
    your_name = forms.CharField(label='Your name', max_length=100)

For more examples and references look into : Django working with forms 有关更多示例和参考,请参阅: Django使用表单

Try like this.. 试试这样..

return render_to_response('register.html', {'form':form}, context_instance=RequestContext(request)) return render_to_response('register.html',{'form':form},context_instance = RequestContext(request))

form is not coming for sure... instead dict as object let's do one iteration like this... 形式不是肯定的...而是作为对象dict让我们做这样的迭代...

Try like this; 试试这样;

form = MyForm(request.POST) if request.method == 'POST': form = MyForm(request.POST)if request.method =='POST':

This is logical error your form should be top. 这是逻辑错误,您的表单应该是顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM