python - 未找到带有参数“(”,)“的“类别”的反向。尝试了 1 种模式:['category/(?P[^/]+)/$']
问题描述
当我| slugify
在 home.html 中添加 a 时,如果我不添加一切正常,我会收到此错误。
另外,当我输入一个类别时,某些类别的帖子不显示,there is no post in this category
而是显示。还有关键的错误不断的弹出,可能是某处有bug或者代码不正确,我还是没找到原因,我解决了一秒,弹出一个错误
模型.py
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
#from datetime import datetime, date
from django.utils import timezone
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=200)
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
post_date = models.DateTimeField(auto_now_add=True)
category = models.CharField(max_length=200, default='разные')
def __str__(self):
return self.title + ' | ' + str(self.author)
def get_absolute_url(self):
return reverse('article_detail', args=[str(self.id)])
视图.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
cats = Category.objects.all()
template_name = 'home.html'
ordering = ['-post_date']
def get_context_data(self, *args, **kwargs):
cat_menu = Category.objects.all()
context = super(HomeView,self).get_context_data(*args, **kwargs)
context["cat_menu"] = cat_menu
return context
def CategoryView(request, cats):
category_posts = Post.objects.filter (category = cats)
return render(request, 'categories.html', {'cats':cats.title(), 'category_posts':category_posts})
class ArticleDetailView(DetailView):
model = Post
template_name = 'post_detail.html'
class AddPostView(CreateView):
model = Post
form_class = PostForm
template_name= 'add_post.html'
#fields = '__all__'
class AddCategoryView(CreateView):
model = Category
template_name= 'add_category.html'
fields = '__all__'
class UpdatePostView(UpdateView):
model = Post
template_name = 'update_post.html'
form_class = EditForm
#fields = ['title', 'body']
class DeletePostView(DeleteView):
model = Post
template_name = 'delete_post.html'
success_url = reverse_lazy('home')
主页.html
{% extends 'base.html' %}
{% block content %}
<h1>Articles</h1>
{% for post in object_list %}
<ul>
<li>
<h2><l1><a href="{% url 'article_detail' post.pk %}">{{ post.title }}</a></h2>
<a href="{% url 'category' post.category|slugify %}">{{ post.category }}</a>
| {{ post.author }}
{% if user.is_authenticated %}
|-<small><a href="{% url 'update_post' post.pk %}">Редакт..)</a></small>
{% endif %}
<br/>{{ post.body|slice:":50"|safe }}
</l1>
</ul>
{% endfor %}
{% endblock %}
网址.py
from django.urls import path
from .views import HomeView, ArticleDetailView, AddPostView, UpdatePostView, DeletePostView, AddCategoryView, CategoryView
urlpatterns = [
path('', HomeView.as_view(), name='home'),
path('article/<int:pk>', ArticleDetailView.as_view(), name='article_detail'),
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>/delete', DeletePostView.as_view(), name='delete_post'),
path('category/<str:cats>/', CategoryView, name='category'),
]
类别.py
{% extends 'base.html' %}
{% block content %}
{% if category_posts %}
<h1>{{cats}}</h1>
{% for post in category_posts %}
<ul>
<l1><a href="{% url 'article_detail' post.pk %}">{{ post.title }}</a>
| {{ post.author }}
{% if user.is_authenticated %}
|-<small><a href="{% url 'update_post' post.pk %}">Редакт..)</a></small>
{% endif %}
<br/>{{ post.body|slice:":50"|safe }}
</l1>
</ul>
{% endfor %}
{% else %}
<h1>So sorry</h1>
{% endif %}
{% endblock %}
请在我的培训地图上帮助我,并为我的致命错误提出解决方案
解决方案
推荐阅读
- openssl - openssl:如何在脚本中将 pfx 转换为 pem
- php - Laravel 5.7 中的 Arr::get() 与 data_get()
- c# - Xamarin.Android:在 AlertDialog 中检索选定的 ListView 元素
- jquery - How to display link conditionally based on the site's language using jQuery
- apache-nifi - 如何在 ReplaceText 处理器中添加多个搜索值和多个替换值?
- arduino - 通过 BluetoothSerial 将 esp32 连接到 hc-06。如何?
- r - 合并 shapefile 中的国家/省以使用 ggplot2 进行绘图
- node.js - 如何在 Node.js 的 Sequelize Mock 中为 Sequelize.js 原始查询编写测试用例
- html - 如何在 html css bootstrap 中创建以下附加模板
- ios - 如何使用 NSLayoutManager 用自定义绘图替换文本附件?