django形成数据库

时间:2010-07-20 18:55:06

标签: python django django-forms

嘿伙计们,我试图制作一个志愿者表格,其中包含姓名,姓氏等信息,我希望将该信息保存到我的数据库(MySQL)中,以便以后可以检索。 / p>

1 个答案:

答案 0 :(得分:2)

首先,您需要在models.py文件中定义一个可以保存此信息的模型:

class Volunteer(models.Model):
    def __unicode__(self):
        return self.fname + self.lname
    fname = models.CharField(max_length=200)
    lname = models.CharField(max_length=200)
    bio = models.TextField(max_length=400)
    number = models.CharField(max_length=15)
    email = modesl.CharField(max_length=255)

然后在views.py中接收来自表单的POST数据的视图:

def volunteer_create(request):
    if request.method == 'POST'and request.POST['fname'] and request.POST['lname'] and request.POST['email'] (ETC...):
        v = Volunteer()
        v.fname = request.POST['fname']
        v.fname = request.POST['lname']
        v.fname = request.POST['email']
        v.fname = request.POST['number']
        ...
        v.save()
        return HttpResponse("Thank you!")    #success!
    else
        return HttpResponseRedirect("/volunteer_form/") #take them back to the form to fill out missed info

然后,您需要设置urls.py以将表单目标指向此视图。