开发者

Getting empty form in Django with MetaForm

I have this as template account_form.html

<form action="/contact/" method="post">
    {% for field in form %}
        <div class="fieldWrapper">
            {{ field.errors }}
            {{ field.label_tag }}: {{ field }}
        </div>
    {% endfor %}
    <p><input type="submit" value="Send message" /></p>
</form>

My model.py

class Account(models.Model):
        person_name     = models.CharField(max_length=30)
        account_number  = models.IntegerField()
        creation_date   = models.DateField()

My View is

def account_form(request):
    if request.method == 'POST': # If the form has been submitted...
        form = AccountForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
                # Process the data in form.cleaned_data
                # ...
                return HttpResponseRedirect('/thanks/') # Redirect after POST
    else:
        form = AccountForm() # An unbound form

    return render_to_response('account_form.html', {
            'form': form,
    })

The problem is whe开发者_JS百科n i load the page i only get the submit button nothing else


I think you forgot to actually create your form:

Should read something like this:

forms.py:

from django.forms import ModelForm
from yourapp.models import Account

class AccountForm(ModelForm):
    class Meta:
        model = Order

This will give you all fields from Account.

HTH

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜