Python 与#x27相反;类别';带参数';(';';,)';没有找到。尝试了1种模式:[';类别/(?P<;cats>;[^/]+;)/$&]
当我在home.html中添加一个Python 与#x27相反;类别';带参数';(';';,)';没有找到。尝试了1种模式:[';类别/(?P<;cats>;[^/]+;)/$&],python,django,django-urls,Python,Django,Django Urls,当我在home.html中添加一个| slagify时,如果我没有添加所有内容,就会出现这个错误 此外,当我输入一个类别时,帖子在某些类别中不显示,此类别中没有帖子。还有一些关键的错误不断弹出,可能是某个地方有bug或者代码不正确,我还是没有找到原因,我解决了一秒钟,一个错误弹出 models.py from django.db import models from django.contrib.auth.models import User from django.urls import re
| slagify
时,如果我没有添加所有内容,就会出现这个错误
此外,当我输入一个类别时,帖子在某些类别中不显示,此类别中没有帖子
。还有一些关键的错误不断弹出,可能是某个地方有bug或者代码不正确,我还是没有找到原因,我解决了一秒钟,一个错误弹出
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)])
views.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')
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 %}
{%extends'base.html%}
{%block content%}
文章
{%用于对象中的post_列表%}
-
|{{post.author}}
{%if user.u经过身份验证%}
|-
{%endif%}
{post.body|slice::50|safe}
{%endfor%}
{%endblock%}
url.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'),
]
从django.url导入路径
从.views导入HomeView、ArticleDetailView、AddPostView、UpdatePostView、DeletePostView、AddCategoryView、CategoryView
URL模式=[
路径(“”,HomeView.as_view(),name='home'),
路径('article/',articletailview.as_view(),name='article_detail'),
路径('add_post/',AddPostView.as_view(),name='add_post'),
路径('add_category/',AddCategoryView.as_view(),name='add_category'),
路径('article/edit/',UpdatePostView.as_view(),name='update_post'),
路径('article//delete',DeletePostView.as_view(),name='delete_post'),
路径('category/',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 %}
{%extends'base.html%}
{%block content%}
{%if类别_posts%}
{{cats}}
{类别_posts%}中的post为%
|{{post.author}}
{%if user.u经过身份验证%}
|-
{%endif%}
{post.body|slice::50|safe}
{%endfor%}
{%else%}
非常抱歉
{%endif%}
{%endblock%}
请在我的培训地图上帮助我,并为我的致命错误提出解决方案在URL.pyReverse中尝试
路径('category/',CategoryView,name='category')
,找不到带参数的'category'('Пззззззз,')。尝试了1个模式:['category/(?P[-a-zA-Z0-9_]+)/$'](是否可能在model.py中由于pk需要进行更改)thnksDid您是否为home.html
提供了正确的模板代码?因为模板似乎需要在其上下文中使用Post
对象({%for Post in object_list%}
),但是HomeView
视图返回Category
对象(cat_menu=Category.objects.all()
)@asd字符串'slugify
将简单地删除它们,因此您将得到一个空字符串'
作为slug。。。