首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用参数(‘’,)‘找不到'article_detail’的反向。1模式尝试:[‘文章/(?P<pk>[0-9]+)$’]

用参数(‘’,)‘找不到'article_detail’的反向。1模式尝试:[‘文章/(?P<pk>[0-9]+)$’]
EN

Stack Overflow用户
提问于 2021-04-09 18:54:11
回答 1查看 647关注 0票数 1

我在不同的模板上经常出错,我写了一个博客,一旦我清除了我的category_detail,一切就都正常了,很明显,中有一个错误。

category_detail.html

代码语言:javascript
复制
{% 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

代码语言:javascript
复制
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')

models.py

代码语言:javascript
复制
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)])

urls.py

代码语言:javascript
复制
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

代码语言:javascript
复制
{% 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 %}

帮助解决这个问题,我已经坐了第二天了,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-09 19:28:09

我想问题在这条线上:

代码语言:javascript
复制
category_posts = Post.objects.filter(category=cats),

您已经将逗号(,)符号放在行尾。因此,category_posts是(,)的元组,在模板中,您要迭代这个元组而不是queryset。改为:

代码语言:javascript
复制
category_posts = Post.objects.filter(category=cats)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67026797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档