【问题标题】:Adding categories to a django blog将类别添加到 django 博客
【发布时间】:2020-11-19 13:48:31
【问题描述】:

我制作了一个简单的 django 博客,我希望它根据帖子所属的类别显示帖子。 但是当我过滤它时它并没有按预期工作

它不是根据类别过滤帖子,而是在 base.html 文件中显示 html 代码。

我的投票/urls.py 文件

from django.urls import path

from .views import HomeView, ArticleDetailView, AddPostView, UpdatePostView
from .views import DeletePostView, AddCategoryView, CategoryView

urlpatterns = [
    path('', HomeView.as_view(), name='home'),
    path('article/<int:pk>/', ArticleDetailView.as_view(), name='article-view'),
    path('add_post/', AddPostView.as_view(), name='add_post'),
    path('add_category/', AddCategoryView.as_view(), name='add_category'),
    path('article/edit/<int:pk>/', UpdatePostView.as_view(), name='update_post'),
    path('article/<int:pk>/remove/', DeletePostView.as_view(), name='delete_post'),
    path('category/<str:categories>/', CategoryView, name='category')
]

我的views.py文件

from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from .models import Post, Category
from .forms import PostForm, EditForm
from django.urls import reverse_lazy
from django.shortcuts import render


class HomeView(ListView):
    model = Post
    template_name = "home.html"
    # ordering = ["-post_date"]
    ordering = ["-id"]


class ArticleDetailView(DetailView):
    model = Post
    template_name = "detail_view.html"


class AddPostView(CreateView):
    model = Post
    form_class = PostForm
    template_name = "add_post.html"
    # fields = "__all__"


class AddCategoryView(CreateView):
    model = Category
    fields = "__all__"
    template_name = "add_category.html"


def CategoryView(request, categories):
    category_posts = Post.objects.filter(category=categories)
    return render(request, "categories.html", {"categories": categories}, {'category_posts': category_posts})


class UpdatePostView(UpdateView):
    model = Post
    form_class = EditForm
    template_name = "update_post.html"
    # fields = ("title", "title_tag", "body")


class DeletePostView(DeleteView):
    model = Post
    template_name = "delete_post.html"
    success_url = reverse_lazy("home")

我的 categories.html 文件

{% extends 'base.html' %}

{% block content %}
<head>
    <title>{{ categories }} category</title>
</head>

<h1>{{ categories }} category</h1>
<hr>

{% for post in category_posts %}
<ul>
    <li>
        <a href="{% url 'article-view' post.pk %}">
            {{ post.title }}</a> - 
        {{ post.category }} - 
        {{ post.author.first_name }}
        {{ post.author.last_name }} - {{ post.post_date }}<br/>
        <small>{{ post.body | slice:":359" | safe }}</small>

        {% if user.is_authenticated %}
        <small><a href="{% url 'update_post' post.pk %}"> (Edit) </a></small> <small><a
            href="{% url 'delete_post' post.pk %}">(Delete)</a></small> <br/>
        {% endif %}
    </li>
</ul>
{% endfor %}

{% endblock %}

我的 models.py 文件

from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse    

class Category(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return (self.name)

    def get_absolute_url(self):
        return reverse("home")


class Post(models.Model):
    title = models.CharField(max_length=255)
    title_tag = models.CharField(max_length=255)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    body = models.TextField(max_length=3500)
    post_date = models.DateField(auto_now_add=True)
    category = models.CharField(max_length=255, default="uncategorized")

    def __str__(self):
        return (self.title + " | " + str(self.author))

    def get_absolute_url(self):
        return reverse("home")

谢谢。

【问题讨论】:

    标签: python django blogs


    【解决方案1】:

    return render() 行中的位置参数有错误。 CategoryView 中的上下文变量应该在一个字典中。

    替换

    {"categories": categories}, {'category_posts': category_posts}
    

    {"categories": categories, 'category_posts': category_posts}
    

    【讨论】:

      猜你喜欢
      • 2021-03-14
      • 2016-04-12
      • 2016-10-27
      • 1970-01-01
      • 2013-08-14
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 2017-01-18
      相关资源
      最近更新 更多