创建一个表单并用django中的数据库中的值填充它

时间:2014-02-27 11:05:49

标签: django django-forms django-templates django-views

我想创建一个用数据库中的值填充的表单

class Venue(models.Model):
venue_Name = models.CharField(max_length=100)
place = models.CharField(max_length=50)
rent = models.IntegerField()
parking_area = models.IntegerField()
picture = models.ImageField(upload_to='images/', blank=True, null=True)

我希望表单显示此处的所有字段

2 个答案:

答案 0 :(得分:1)

1,输入

forms.py

from django.forms import ModelForm
from youapp.models import Venue

class VenueForm(ModelForm):
    class Meta:
        model = Venue

views.py

from youapp.forms import VenueForm

from django.shortcuts import render
from django.http import HttpResponseRedirect

def contact(request):
    form = VenueForm()
    if request.method == 'POST': # If the form has been submitted...
        # VenueForm was defined in the the previous section
        form = VenueForm(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


    return render(request, 'contact.html', {
        'form': form,
    })

template.html

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

见这里:https://docs.djangoproject.com/en/1.6/topics/forms/

<强> 2.输出

from django.views.generic import ListView,DetailView

见这里:https://docs.djangoproject.com/en/dev/topics/class-based-views/generic-display/

答案 1 :(得分:0)