这是模型关系:
class Tag(models.Model):
name = models.CharField(max_length=100)
description = models.CharField(max_length=500, null=True, blank=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now_add=True)
class Post(models.Model):
user = models.ForeignKey(User)
tagfield = models.ManyToManyField(Tag)
title = models.CharField(max_length=100)
content = models.TextField()
created = models.DateTimeField(default=datetime.datetime.now)
modified = models.DateTimeField(default=datetime.datetime.now)
def __unicode__(self):
return '%s,%s' % (self.title,self.content)
class PostModelForm(forms.ModelForm):
class Meta:
model = Post
class PostModelFormNormalUser(forms.ModelForm):
class Meta:
model = Post
exclude = ('user', 'created', 'modified')
在views.py中:
form = PostModelFormNormalUser()
context = {'form':form}
return render_to_response('addpost.html', context, context_instance=RequestContext(request))
in add.html:`{{form.as_p}}
标题,内容和选择输入正在网页中显示。
<p><label for="id_tagfield">Tagfield:</label> <select multiple="multiple" name="tagfield" id="id_tagfield">
</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>
如何获取文本框而不是name = models.CharField(max_length=100)
的输入。
Tag.name将由空格分隔。我需要在网页上显示“标题,内容,标记名称”。
答案 0 :(得分:0)
你会想要这样的东西:
class PostModelFormNormalUser(forms.ModelForm):
class Meta:
model = Post
exclude = ('user', 'created', 'modified')
widgets = {
'tagfield': Textinput(),
}
然后你需要在视图中自己处理POST数据。