我如何获取文本字段而不是

发布于 2025-01-05 16:47:41 字数 1611 浏览 1 评论 0原文

这是模型关系:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝梦月影 2025-01-12 16:47:41

请参阅 https:// /docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets

你会想要这样的东西:

class PostModelFormNormalUser(forms.ModelForm):
    class Meta:
        model = Post
        exclude = ('user', 'created', 'modified')
        widgets = {
            'tagfield': Textinput(),
        }

然后您需要自己在视图中处理 POST 数据。

See https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets

You'll want something like this:

class PostModelFormNormalUser(forms.ModelForm):
    class Meta:
        model = Post
        exclude = ('user', 'created', 'modified')
        widgets = {
            'tagfield': Textinput(),
        }

Then you'll need to handle the POST data yourself in the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文