首页 > 解决方案 > 未找到带有参数“(”,)“的“类别”的反向。尝试了 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 %}

请在我的培训地图上帮助我,并为我的致命错误提出解决方案

标签: pythondjangodjango-urls

解决方案


推荐阅读