1
我的models.py中有一個名爲INPUT的模型。它有多對多的字段 我的問題是,我的值沒有得到保存在數據庫或表名INPUT 我不知道我在哪裏錯了。值未保存在我的模型中
我的models.py是:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Person(models.Model):
title=models.CharField(max_length=1000,unique=True)
cost=models.CharField(max_length=100)
desc=models.TextField(blank=True)
color=models.CharField(max_length=1000)
image_urls=models.CharField(max_length=1000)
source=models.CharField(max_length=1000,unique=True)
category=models.CharField(max_length=1000)
size=models.CharField(max_length=1000)
fit=models.CharField(max_length=1000)
fabric=models.CharField(max_length=1000)
type_dress=models.CharField(max_length=1000)
currency=models.CharField(max_length=1000)
advertiser=models.CharField(max_length=1000)
def __unicode__(self):
return self.title
class foroccasion(models.Model):
occasions=models.CharField(max_length=2000)
def __unicode__(self):
return self.occasions
class forstyle(models.Model):
style=models.CharField(max_length=2000)
def __unicode__(self):
return self.style
class forbodytype(models.Model):
bodytype=models.CharField(max_length=2000)
def __unicode__(self):
return self.bodytype
class Input(models.Model):
tabbody=[
("apple","apple"),
("pear/triangle","pear/triangle"),
("hourglass","hourglass"),
("rectangle","rectangle"),
("inverted triangle","invered triangle")
]
tabstyle=[
("vintage","vintage"),
("sophisticated","sophisticated"),
("chic","chic"),
("bone","bone"),
("edgy","edgy")
]
# apparelid=models.(max_length=104)
apparelid=models.ForeignKey('Person')
# apparelid=models.CharField(max_length=100,blank=True)
userid=models.CharField(max_length=100,blank=True,editable=False)
occassion=models.ManyToManyField(foroccasion)
bodytype=models.ManyToManyField(forbodytype)
style=models.ManyToManyField(forstyle)
def __unicode__(self):
return self.userid
我forms.py如下:
import re
from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from apple3.models import models
from django.forms import ModelForm
from apple3.models import Input,Person
class UserForm(ModelForm):
class Meta:
model=Input
我views.py如下:
def title(request,title_id):
cur=0
cur=Person.objects.get(pk=title_id)
# imge=Person.objects.all()[:109]
# for i in range(1,len(imge)-2):
# if i==int(title_id):
# cur=imge[i]
sz=str(cur.size)
sz=sz.split(',')
fsz=[]
for i in sz:
s=""
for j in range(0,len(i)):
if i[j]!='U' and i[j]!='K' and i[j]!=',':
s+=i[j]
fsz.append(s)
lsz=len(fsz)
#form=UserForm(request.POST)
if request.method == 'POST':# If the form has been submitted...
print "dsflkjdfslkjdsl"
form=UserForm(request.POST)# A form bound to the POST data
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
# ...
#form.cleaned_data()
new=form.save(commit=False)
new.userid=request.user
new.occassion=self.cleaned_data["occassion"]
new.bodytype=self.cleaned_data["bodytype"]
new.style=self.cleaned_data["style"]
new.apparelid=self.cleaned_data["apparelid"]
new.save()
form.save_m2m()
else:
form = UserForm() # An unbound form"""
template=loader.get_template('apple3/title.html')
context=RequestContext(request,{
'cur':cur,
'sz':fsz,
'lsz':lsz,
'form':form,
}
)
return HttpResponse(template.render(context))
我的模板如下:
<form action="/dress/female/" method="post">{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Submit" />
</form>
此外,我已嘗試顯示窗體的單個元素,但它沒有得到保存在數據庫中 請幫我 我是新來的django。
您是否已經驗證的形式是如果form.is_valid():語句有效並運行True塊: – schillingt
你是什麼意思,通過驗證表格 的有效性我寫了form.is_valid()if條件和什麼是真正的塊? – ashishk
你知道代碼「new = form.save(commit = False)」實際上是在執行嗎? 「form.is_valid()」返回True嗎? – schillingt