我正在创建一个图片共享网站....
问题1:
这是图像搜索页面的屏幕截图
enter image description here
这里的问题是,当我按标签搜索时,会出现错误消息:Related Field got invalid lookup: icontains
问题2:这是图像上传页面的屏幕截图 enter image description here 问题是,我只能选择标签但不能在这里输入新标签。我希望它能够让图像的上传者输入最多10个标签....
以下是我的代码:
models.py
class Tag(models.Model):
tag_name = models.CharField(max_length=50)
def __str__(self):
return self.tag_name
class Image(models.Model):
category = models.ForeignKey(Category, blank=True, null=True, on_delete=models.DO_NOTHING)
in_gallery = models.ManyToManyField(Gallery, blank=True)
tag = models.ManyToManyField(Tag, blank=True)
title = models.CharField(max_length=100)
photo = models.FileField()
description = models.CharField(max_length=1000)
def __str__(self):
return self.title + '-' + self.description
def regions_changed(sender, **kwargs):
if kwargs['instance'].tag.count() > 1:
raise ValidationError("You can't assign more than three regions")
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)
filters.py
class ImageFilter(django_filters.FilterSet):
title = django_filters.CharFilter(lookup_expr='icontains')
description = django_filters.CharFilter(lookup_expr='icontains')
tag = django_filters.CharFilter(lookup_expr='icontains')
class Meta:
model = Image
fields = ['title', 'description', 'category', 'tag']
class NullFilter(django_filters.FilterSet):
class Meta:
model = Image
fields = ['upload_date']
views.py
class ImageCreate (CreateView):
model = Image
fields = ['category', 'title', 'photo', 'description', 'tag']
def form_valid(self, form):
form.instance.owner = self.request.user.profile
return super().form_valid(form)
image_form.html
<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>