找不到参数“('',)”的“article_detail”的反向操作。尝试了 1 个模式:['article/(?P<pk>[0-9]+)$']

问题描述 投票:0回答:2

我在不同的模板上经常出现错误,我创建了一个博客,一旦我清除了我的category_detail,那么一切就正常了,显然其中有一个错误

category_detail.html

{% extends 'base.html' %}
{% block content %}

{% if category_posts %}


  <h1>{{ cat }}</h1>


  <ul>
    {% for post in category_posts %}
  <l1><a href="{% url 'article_detail' post.pk %}">{{ post.title }}</a>
    {{ post.category }}
     | {{ post.author }}
     {% if user.is_authenticated %}
     |-<small><a href="{% url 'update_post' post.pk %}">Редакт..)</a></small>
     {% endif %}
     <br/>{{ post.body|slice:":50"|safe }}
   </l1>
   {% endfor %}
   </ul>


 {% else %}
 <h1>Извините страница не найдена</h1>
 {% endif %}
{% endblock %}

如果我不添加类别的代码和模板,一切正常。 输入代码

views.py

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

def CategoryView(request, cats):
    category_posts = Post.objects.filter(category=cats),
    return render(request, 'category_detail.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')

模型.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)])

url.py

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('category/<str:cats>/', CategoryView, name='category'),
    path('article/edit/<int:pk>', UpdatePostView.as_view(), name='update_post'),
    path('article/<int:pk>/delete', DeletePostView.as_view(), name='delete_post'),


]

home.html

{% extends 'base.html' %}
{% block content %}


<h1>Articles</h1>


{% for post in object_list %}


<ul>
    <l1><a href="{% url 'article_detail' post.pk %}">{{ post.title }}</a>
      <a href="{% url 'category' post.category %}">{{ 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 %}

帮忙解决问题,我已经坐了第二天了

python django reverse
2个回答
2
投票

我认为问题出在这一行:

category_posts = Post.objects.filter(category=cats),

您已在行尾添加了 逗号(,) 符号。因此,

category_posts
是 (,) 的元组,并且在模板中,您正在迭代这个元组而不是查询集。更改为:

category_posts = Post.objects.filter(category=cats)

0
投票
 def get_absolute_url(self):
    return reverse('article_detail', args=[str(pk)])

我认为你需要写

pk
而不是
self.id

© www.soinside.com 2019 - 2024. All rights reserved.