【问题标题】:creating a form and populating it with values in database in django在 django 中创建一个表单并用数据库中的值填充它
【发布时间】:2014-03-30 18:27:12
【问题描述】:

我想制作一个用数据库中的值填充的表单

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)

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

【问题讨论】:

  • 请阅读model forms 上的文档,如果您有具体问题,请返回。

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


【解决方案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,
    })

模板.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/

【讨论】:

  • 提问者必须接受更相关的答案。
  • 首先,我要求的恰恰相反。我想以 html 格式打印数据库中存在的数据,这样当我选择场地时,我会得到选择哪个场地的信息
【解决方案2】:
猜你喜欢
  • 2012-09-05
  • 1970-01-01
  • 2021-01-01
  • 1970-01-01
  • 2019-02-17
  • 2015-01-23
  • 1970-01-01
  • 2011-06-14
  • 2013-10-25
相关资源
最近更新 更多