我如何获取文本字段而不是
这是模型关系:
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))
在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 是由空格分隔的字符串。我需要“标题、内容、标记名”才能在网页上显示。
This is the model relation:
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')
in views.py:
form = PostModelFormNormalUser()
context = {'form':form}
return render_to_response('addpost.html', context, context_instance=RequestContext(request))
in add.html: `{{ form.as_p }}
The title,content and select input is being displayed in webpage.
<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>
How can i get textbox instead of input for name = models.CharField(max_length=100)
.
Tag.name would be string seperated by spaces. I need to 'title,content,tagname' to display on webpage.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅 https:// /docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets
你会想要这样的东西:
然后您需要自己在视图中处理 POST 数据。
See https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets
You'll want something like this:
Then you'll need to handle the POST data yourself in the view.