Django:表单模板:输入字段但不选择ManyToMany Relations

时间:2018-04-24 07:52:17

标签: python django

我正在创建一个供用户上传图片的网站......这是表单页面: this is the screencap of the resulting page

从上面的screencap中,我无法输入任何标签,我只能选择现有的标签

我的问题是:要添加到代码中的内容,以便用户在上传图片时最多可输入10个代码

models.py

class Image(models.Model):
    owner = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
    category = models.ForeignKey(Category,  blank=True, null=True, on_delete=models.DO_NOTHING)
    tag = models.ManyToManyField(Tag,  blank=True, null=True)
    title = models.CharField(max_length=100)
    photo = models.FileField()
    description = models.CharField(max_length=1000)
    def __str__(self):
        return self.title + '-' + self.description

class Tag(models.Model):
    tag_name = models.CharField(max_length=50)

class ImageTag (models.Model):
    image = models.ForeignKey(Image,  blank=True, null=True, on_delete=models.DO_NOTHING)
    tag = models.ForeignKey(Tag,  blank=True, null=True, on_delete=models.DO_NOTHING)

urls.py

url(r'image/add/$', views.ImageCreate.as_view(), name='image-add'),

view.py

class ImageCreate (CreateView):
    model = Image
    fields = ['category', 'title', 'photo', 'description', 'tag']

image_form.html(我删除了不相关的div)

<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {% include 'imagebank/form-template.html' %}
            <button type="submit" class="btn btn-success">Submit</button>
</form>

0 个答案:

没有答案