我正在尝试在django的主页上列出所有具有相同类别的帖子。我希望标题成为类别标题,然后在标题下方显示所有与该类别相关的帖子。我通过在stackoverflow上寻求帮助在基于类的视图中做到了这一点,但我想在基于函数的视图中了解这一点,以了解这一点。
posts / models.py
from tinymce import HTMLField
from django.db import models
from django.contrib.auth import get_user_model
from slugger import AutoSlugField
from django.urls import reverse
# Create your models here.
User = get_user_model()
def upload_location(instance, filename):
return "%s/%s" %(instance.slug, filename)
class Author(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
def __str__(self):
return self.user.username
class Category(models.Model):
title = models.CharField(max_length=20)
slug = AutoSlugField(populate_from='title')
def __str__(self):
return self.title
class Post(models.Model):
title = models.CharField(max_length = 100)
slug = AutoSlugField(populate_from='title')
overview = models.CharField(max_length= 200)
timestamp = models.DateTimeField(auto_now_add=True)
content = HTMLField()
comment_count = models.IntegerField(default=0)
view_count = models.IntegerField(default=0)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
thumbnail = models.ImageField(
upload_to=upload_location,
null=True,
blank=True)
category = models.ManyToManyField(Category)
featured = models.BooleanField()
previous_post = models.ForeignKey('self', related_name= 'previous', on_delete=models.SET_NULL, blank=True, null=True)
next_post = models.ForeignKey('self', related_name= 'next', on_delete=models.SET_NULL, blank=True, null=True)
def __str__(self):
return self.title
posts / views.py我尝试执行此操作,但这给我这样的错误
TypeError位于/ index()缺少1个必需的位置参数:“ category_slug”
from django.shortcuts import render
from .models import Post, Author, Category
# Create your views here.
def index(request):
featured = Post.objects.filter(featured = True) #put this on carousel
latest_post = Post.objects.order_by('-timestamp')[:6]
category = Category.objects.filter(slug= category_slug)
post_by_category = Post.objects.filter(category=category)
context = {
'object_list': featured,
'latest_post': latest_post,
'post_by_category': post_by_category,
}
return render(request, 'index.html', context)
urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
from posts.views import index
urlpatterns = [
path('admin/', admin.site.urls),
path('', index),
path('tinymce/', include('tinymce.urls'))
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
答案 0 :(得分:0)
在使用基于函数的视图(如def index(request, category_slug):
)时,您需要声明一个query_param,并且必须在URL中捕获它。试试这个:
path('<str:category_slug>', index)
作为您的索引视图
答案 1 :(得分:0)
因此,这是您的网址,您可以从其中获取类别信息以过滤帖子
path('<str:category_slug>', index)
您的请求函数必须将url参数作为参数,以便您可以从数据库中查询类别,在这种情况下,该类别为category_slug
def index(request, category_slug):
category = Category.objects.get(slug=category_slug)
post_by_category = Post.objects.filter(category=category)
请记住,filter()返回实例列表,因此请改用get()。