对 'article_detail' 进行反转,但未找到参数 '('',)'。尝试了 1 种模式:['article/(?P<pk>[0-9]+)$']
Reverse for 'article_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['article/(?P<pk>[0-9]+)$']
我在不同的模板上经常出错,我写了一个博客,一旦我清除了我的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')
models.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)])
urls.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 %}
帮忙解决问题,坐了第二天
我认为问题出在这一行:
category_posts = Post.objects.filter(category=cats),
您在行尾放置了 逗号(,) 符号。因此,category_posts
是 (,) 的元组,并且在模板中迭代此元组而不是查询集。将其更改为:
category_posts = Post.objects.filter(category=cats)
我在不同的模板上经常出错,我写了一个博客,一旦我清除了我的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')
models.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)])
urls.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 %}
帮忙解决问题,坐了第二天
我认为问题出在这一行:
category_posts = Post.objects.filter(category=cats),
您在行尾放置了 逗号(,) 符号。因此,category_posts
是 (
category_posts = Post.objects.filter(category=cats)