我已经添加了分类和所有内容,但是我在分类中看不到任何帖子。当我打开例如http://localhost:8000/category/sport/时,它显示没有帖子...
my urls.py :
from django.urls import path
#from . import views
from .views import HomeView , ArticleDetailView, AddPostView, UpdatePostView, DeletePostView, CategoryView
urlpatterns = [
#path('', views.home, name="homepage"),
path('', HomeView.as_view(), name = 'home'),
path('article/<int:pk>', ArticleDetailView.as_view(), name = 'article-details'),
path('add_post/', AddPostView.as_view(), name = 'add_post'),
path('article/edit/<int:pk>', UpdatePostView.as_view(), name = 'update_post'),
path('article/<int:pk>/delete', DeletePostView.as_view(), name = 'delete_post'),
path('category/<str:categories>/', CategoryView, name='category'),
]
my models.py :
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from datetime import datetime, date
class Category(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
def get_absolute_url(self):
#return reverse('article-details', args=(str(self.id)))
return reverse('home')
class Post(models.Model):
title = models.CharField(max_length=255)
title_tag = models.CharField(max_length=255, default="YNTN")
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
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")
m
y views.py :
from django.shortcuts import render
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
#def home(request):
# return render(request, 'home.html', {})
class HomeView(ListView):
model = Post
template_name = 'home.html'
#ordering = ['-id']
ordering = ['-post_date']
def CategoryView(request, categories):
category_posts = Post.objects.filter(category=categories)
return render(request, "categories.html", {"categories": categories, 'category_posts': category_posts})
class ArticleDetailView(DetailView):
model = Post
template_name = 'article.details.html'
class AddPostView(CreateView):
model = Post
form_class = PostForm
template_name = 'add_post.html'
#fields = '__all__'
#fields = ('title', 'body')
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')
我的分类. 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-details' 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 %}
my forms.py :
from django import forms
from .models import Post, Category
choices = Category.objects.all().values_list('name', 'name')
choice_list = []
for item in choices:
choice_list.append(item)
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('title', 'title_tag', 'author', 'category', 'body')
widgets = {
'title': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Enter the title of your article'}),
'title_tag': forms.TextInput(attrs={'class': 'form-control'}),
'author': forms.Select(attrs={'class': 'form-control',}),
'category': forms.Select(choices=choice_list , attrs={'class': 'form-control',}),
'body': forms.Textarea(attrs={'class': 'form-control', 'placeholder': 'Enter the content of your article'}),
}
class EditForm(forms.ModelForm):
class Meta:
model = Post
fields = ('title', 'title_tag', 'author', 'body')
widgets = {
'title': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Enter the title of your article'}),
'title_tag': forms.TextInput(attrs={'class': 'form-control'}),
'author': forms.Select(attrs={'class': 'form-control',}),
'body': forms.Textarea(attrs={'class': 'form-control', 'placeholder': 'Enter the content of your article'}),
}
请帮帮忙,这对我很重要:D我真的卡住了。这是我的第一篇StackOverflow帖子:D
1条答案
按热度按时间5uzkadbs1#
正确的解决方法是将
Post
模型与Category
关联:models.py
可以使用
<str:category_name>
,但请注意名称必须对应。urls.py
编写代码时请注意良好做法。在
Python
中,类使用CamelCase
,函数使用snake_case
。views.py: