Django二合一混乱

时间:2018-03-24 16:43:32

标签: django django-models

我正在尝试按如下方式显示博客:

  1. domain.com/blog/category/categoryA

  2. domain.com/blog/category/categoryA/post-one

  3. 目前(1)成功运作,(2)部分工作,因此显示如下:

    domain.com/blog/post-one

    如何设置帖子类别 - 显示post slug之前属于该类别的帖子?

    我的urls.py:

            url(r'^(?P<slug>[-\w]+)/$', views.post_detail, name='post_detail'),
            url(r'^category/(?P<category_slug>[-\w]+)/$', views.list_of_post_by_category, name='list_of_post_by_category'),
    

    我的views.py

    def list_of_post_by_category(request,category_slug):
        categories = Category.objects.all()
        post = Post.objects.filter(status='published')
        if category_slug:
            category = get_object_or_404(Category, slug=category_slug)
            post = post.filter(category=category)
        template = 'blog/category/list_of_post_by_category.html'
        context = {'categories': categories, 'post': post}
        return render(request, template, context)
    
    
    def list_of_post(request):
        post = Post.objects.filter(status="published")
        template = 'blog/post/list_of_post.html'
        context = {'post': post}
        return render(request, template, context)
    
    
    def post_detail(request, slug):
        post = get_object_or_404(Post, slug=slug)
        template = 'blog/post/post_detail.html'
        context = {'post': post}
        return render(request, template, context)
    

    我的models.py:

    class Category(models.Model):
        name = models.CharField(max_length=250)
        slug = models.SlugField(max_length=250, unique=True)
    
        class Meta:
            ordering = ('name',)
            verbose_name = 'category'
            verbose_name_plural = 'categories'
    
        def get_absoulte_url(self):
            return reverse('blogCMSApp:list_of_post_by_category', args=[self.slug])
    
        def __str__(self):
            return self.name
    
    # Posts
    class Post(models.Model):
        STATUS_CHOICES = (
            ('draft', 'Draft'),
            ('published', 'Published')
        )
        category = models.ForeignKey(Category, on_delete=models.PROTECT)
        title = models.CharField(max_length=250)
        slug = models.SlugField(max_length=250, unique=True)
        content = models.TextField()
        seo_title = models.CharField(max_length=250)
        seo_description = models.CharField(max_length=160)
        author = models.ForeignKey(User, related_name='blog_posts', on_delete=models.PROTECT)
        published = models.DateTimeField(default=timezone.now)
        created = models.DateTimeField(auto_now_add=True)
        updated = models.DateTimeField(auto_now=True)
        status = models.CharField(max_length=9, choices=STATUS_CHOICES, default='draft')
    
    def get_absoulte_url(self):
        return reverse('blogCMSApp:post_detail', args=[self.slug])
    
    def __str__(self):
        return self.title
    

1 个答案:

答案 0 :(得分:0)

您需要在urls.py中查找该模式,然后您需要为帖子链接构建正确的网址:

url(
    r'^category/(?P<category_slug>[-\w]+)/(?P<slug>[-\w]+)/$', 
    views.post_detail, 
    name='post_detail'
),
url(
    r'^category/(?P<category_slug>[-\w]+)/$',
    views.list_of_post_by_category, 
    name='list_of_post_by_category'
),

Post模型中:

def get_absoulte_url(self):
    return reverse(
        'blogCMSApp:post_detail', 
        args=[self.category.slug, self.slug]
    )