我想获得所有具有相似名称的壁纸,与渲染的壁纸标签
I want to get all the wallpapers who have similar name, tags to the wallpaper which is rendered
views.py
def download(request, wallpaper_name):
wallpaper = Wallpaper.objects.get(name=wallpaper_name)
context = {'wallpaper': wallpaper}
return render(request, 'Wallpaper/download.html', context)
models.py
class Tags(models.Model):
tag = models.CharField(max_length=100)
壁纸模型
class Wallpaper(models.Model):
name = models.CharField(max_length=100, null=True)
size = models.CharField(max_length=50, null=True)
pub_date = models.DateField('date published', null=True)
resolution = models.CharField(max_length=100, null=True)
category = models.ManyToManyField(Category)
tags = models.ManyToManyField(Tags)
HTML
<ul>
<li>{{wallpaper.name}}</li>
{% for i in wallpaper.tags_set.all %}
<li>{{i.tags}}</li>
{% endfor %}
</ul>
urls.py
path('<wallpaper_name>/', views.download, name="download"),
比如我选择下载的壁纸有自然和海洋标签所以我想要所有有这个标签的壁纸
应该是
<ul>
<li>{{wallpaper.name}}</li>
{% for i in wallpaper.tags.all %}
<li>{{i.tags}}</li>
{% endfor %}
</ul>
而不是
<ul>
<li>{{wallpaper.name}}</li>
{% for i in wallpaper.tags_set.all %}
<li>{{i.tags}}</li>
{% endfor %}
</ul>
要获取与标签相关的墙纸,您可以这样做
def download(request, wallpaper_name):
wallpaper = Wallpaper.objects.get(name=wallpaper_name)
related_wallpaper = Wallpaper.objects.filter(tags=wallpaper.tags.all())
context = {'wallpaper': wallpaper,'related_wallpaper':related_wallpaper}
return render(request, 'Wallpaper/download.html', context)
如果你想根据标签获得与你拥有的壁纸相似的壁纸,你应该使用这个库 django-taggit。
这个库的 API 有一个方法 similar_objects()
。
这就是您的墙纸模型的样子。
from taggit.managers import TaggableManager
class Wallpaper(models.Model):
name = models.CharField(max_length=100, null=True)
size = models.CharField(max_length=50, null=True)
pub_date = models.DateField('date published', null=True)
resolution = models.CharField(max_length=100, null=True)
category = models.ManyToManyField(Category)
tags = TaggableManager()
在你的 views.py
def download(request, wallpaper_name):
wallpaper = Wallpaper.objects.get(name=wallpaper_name)
similar_wallpapers = wallpaper.tags.similar_objects()
context = {'wallpaper': wallpaper, 'similar_wallpapers': similar_wallpapers}
return render(request, 'Wallpaper/download.html', context)
HTML
<ul>
<li>{{wallpaper.name}}</li>
{% for wallpaper in similar_wallpapers %}
<li>{{wallpaper.name}}</li>
{% endfor %}
</ul>
这将列出具有相同或相似标签的壁纸的名称。
views.py
def download(request, wallpaper_name):
wallpaper = Wallpaper.objects.get(name=wallpaper_name)
context = {'wallpaper': wallpaper}
return render(request, 'Wallpaper/download.html', context)
models.py
class Tags(models.Model):
tag = models.CharField(max_length=100)
壁纸模型
class Wallpaper(models.Model):
name = models.CharField(max_length=100, null=True)
size = models.CharField(max_length=50, null=True)
pub_date = models.DateField('date published', null=True)
resolution = models.CharField(max_length=100, null=True)
category = models.ManyToManyField(Category)
tags = models.ManyToManyField(Tags)
HTML
<ul>
<li>{{wallpaper.name}}</li>
{% for i in wallpaper.tags_set.all %}
<li>{{i.tags}}</li>
{% endfor %}
</ul>
urls.py
path('<wallpaper_name>/', views.download, name="download"),
比如我选择下载的壁纸有自然和海洋标签所以我想要所有有这个标签的壁纸
应该是
<ul>
<li>{{wallpaper.name}}</li>
{% for i in wallpaper.tags.all %}
<li>{{i.tags}}</li>
{% endfor %}
</ul>
而不是
<ul>
<li>{{wallpaper.name}}</li>
{% for i in wallpaper.tags_set.all %}
<li>{{i.tags}}</li>
{% endfor %}
</ul>
要获取与标签相关的墙纸,您可以这样做
def download(request, wallpaper_name):
wallpaper = Wallpaper.objects.get(name=wallpaper_name)
related_wallpaper = Wallpaper.objects.filter(tags=wallpaper.tags.all())
context = {'wallpaper': wallpaper,'related_wallpaper':related_wallpaper}
return render(request, 'Wallpaper/download.html', context)
如果你想根据标签获得与你拥有的壁纸相似的壁纸,你应该使用这个库 django-taggit。
这个库的 API 有一个方法 similar_objects()
。
这就是您的墙纸模型的样子。
from taggit.managers import TaggableManager
class Wallpaper(models.Model):
name = models.CharField(max_length=100, null=True)
size = models.CharField(max_length=50, null=True)
pub_date = models.DateField('date published', null=True)
resolution = models.CharField(max_length=100, null=True)
category = models.ManyToManyField(Category)
tags = TaggableManager()
在你的 views.py
def download(request, wallpaper_name):
wallpaper = Wallpaper.objects.get(name=wallpaper_name)
similar_wallpapers = wallpaper.tags.similar_objects()
context = {'wallpaper': wallpaper, 'similar_wallpapers': similar_wallpapers}
return render(request, 'Wallpaper/download.html', context)
HTML
<ul>
<li>{{wallpaper.name}}</li>
{% for wallpaper in similar_wallpapers %}
<li>{{wallpaper.name}}</li>
{% endfor %}
</ul>
这将列出具有相同或相似标签的壁纸的名称。