'Category'对象不可迭代



我是编程新手我有不同的类别在我的django网站,我创建了模型,视图。但是当我尝试去localhost/category/categoryname时,我得到了错误:" category object is not iterable"我提前感谢你的帮助

#url.py
urlpatterns = [
path('', home, name='home'),
path('article/<slug:slug>', detail, name='detail'),
path('article', article, name='article'),
path('category/<slug:slug>', category, name='category')]

###############################################

#views.py
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, JsonResponse, Http404
from .models import Article, Category

# Create your views here.
def home(request):
context = {
"articles": Article.objects.filter(status="Published")
}
return render(request, 'website/home.html', context)

def detail(request, slug):
context = {
"article": get_object_or_404(Article, slug=slug, status="Published")
}
return render(request, 'website/detail.html', context)

def article(request):
context = {
"articles": Article.objects.filter(status="Published"),
"category": Category.objects.filter(status=True)
}
return render(request, 'website/article.html', context)

def category(request, slug):
context = {
"category": get_object_or_404(Category, slug=slug, status=True)
}
return render(request, 'website/category.html', context)

###############################

#models.py
from django.db import models
from django.utils import timezone

# Create your models here.
class Category(models.Model):
title = models.CharField(max_length=300, verbose_name="Category Topic")
slug = models.SlugField(max_length=100, unique=True, verbose_name="Category Address")
status = models.BooleanField(default=True, verbose_name="Do you want to show?")
position = models.IntegerField(verbose_name="position")
class Meta:
verbose_name = "Category"
verbose_name_plural = "Categories"
ordering = ['position']
def __str__(self):
return self.title

class Article(models.Model):
STATUS_CHOICES = (
('Draft', 'Draft'),
('Published', 'Published')
)
title = models.CharField(max_length=300)
slug = models.SlugField(max_length=100, unique=True)
category = models.ManyToManyField(Category, verbose_name="Category", related_name="articles")
description = models.TextField()
thumbnail = models.ImageField(upload_to="images")
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES)
class Meta:
ordering = ['-publish']
def __str__(self):
return self.title

######################

#category.html
{% for article in category.articles.all %}
<article class="entry" data-aos="fade-up">
<div class="entry-img">
<img src="{{ article.thumbnail.url }}" alt="" class="img-fluid">
</div>
<h2 class="entry-title">
{{ article.title }}
</h2>
<div class="entry-meta">
<ul>
<li class="d-flex align-items-center"><i class="icofont-user"></i>
<a href="detail.html">John Doe</a></li>
<li class="d-flex align-items-center"><i class="icofont-wall-clock"></i>
<a href="detail.html"><time>{{ article.publish }}</time></a></li>
<li class="d-flex align-items-center"><i class="icofont-tags"></i>
<ul class="tags">
{% for cat in article.category.all %}
<a href="#">{{ cat.title }}</a>
{% endfor %}
</ul>
</li>
<li class="d-flex align-items-center"><i class="icofont-comment"></i>
<a href="detail.html">12 Comments</a></li>
</ul>
</div>
<div class="entry-content">
{{ article.description|truncatewords:30}}
<div class="read-more">
<a href="{% url 'website:detail' article.slug %}">Read More</a>
</div>
</div>
</article><!-- End blog entry -->
{% endfor %}

您的代码很浅-请这样做以检索类别列表

def get_queryset(self):category =              
get_object_or_404(Category, slug=self.kwargs.get("slug"))                                               
return Post.objects.filter(category_id=category).order_by("-created")

可以使用它按特定列表检索类别Article.objects.filter(post__category__contains='politics'),

我改变了视图,问题解决了:

def category(request, slug):
cat = get_object_or_404(Category, slug=slug, status=True)
context = {
"category": cat.articles.all()
}
return render(request, 'website/category.html', context)

相关内容

  • 没有找到相关文章

最新更新