Django - 找不到页面 (404) urls.py
Django - Page not found (404) urls.py
问题:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/home/category/coding/
Raised by: blogapp.views.BlogDetailView
urls.py
from django.urls.conf import path
from blogapp.views import BlogDetailView, BlogListView,searchposts,CatListView
from . import views
app_name = "blogapp"
urlpatterns = [
path('', views.BlogListView, name="blogs"),
path('home/search/', views.searchposts, name= "searchposts"),
path('home/category/<slug:_id>/', views.BlogDetailView, name="blog"),
path('home/category/<category>/', views.CatListView.as_view(), name="category"),
]
如果我颠倒这两个
path('home/category/<slug:_id>/', views.BlogDetailView, name="blog"),
path('home/category/<category>/', views.CatListView.as_view(), name="category"),
显示类别的html,以及'path('home/category/slug:_id/',views.BlogDetailView的路径, name="blog"),' 未显示,反之亦然
这里是导航栏
的html
{% for category in category_list %}
{% if forloop.counter < 4 %}
<li class="nav-item">
<a class="nav-link" href="home/category/{{category.name}}">{{ category.name|title }}</a>
</li>
{% endif %}
{% endfor %}
我要渲染的html
{% extends "_layouts/base.html" %}
{% block css %}
{{block.super}}
{% endblock css %}
{% block page_title %}Insert_your_name_here{% endblock %}
{% load static %} <!-- load the image -->
{% block maincontent %}
<!-- Banner -->
{% block banner %}
{% load static %}
{% include "layout/category_banner.html" %}
{% endblock %}
<!-- Banner Here -->
!-- This is the main template -->
<section class="blog-posts grid-system">
<div class="container">
<div class="row">
<div class="col-lg-8">
<div class="all-blog-posts">
<div class="row">
{% block categories-main %}
{% include "layout/category.html" %}
{% endblock %}
</div>
</div>
</div>
{% block sidebars %}
{% load static %}
{% include "layout/Sidebars.html" %}
{% endblock %}
</div>
</div>
</section>
{% endblock %}
这是我的views.py
def BlogDetailView(request,_id):
try:
category_list = Category.objects.exclude(name='default')
dataset = BlogModel.newmanager.all()
data = BlogModel.newmanager.get(slug =_id)
comments = CommentModel.objects.filter(blog = data, status=True)
except BlogModel.DoesNotExist:
raise Http404('Data does not exist')
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
Comment = CommentModel(your_name= form.cleaned_data['your_name'],
comment = form.cleaned_data['comment'],
blog=data)
Comment.save()
return redirect(f'/home/category/{_id}')
else:
form = CommentForm()
context = {
'data':data,
'form':form,
'comments':comments,
'dataset': dataset,
"category_list": category_list,
}
return render(request,'layout/post-details-main.html',context)
def searchposts(request):
dataset = BlogModel.newmanager.all()
if request.method == 'GET':
query= request.GET.get('q')
submitbutton= request.GET.get('submit')
if query is not None:
lookups= Q(blog_title__icontains=query) | Q(blog__icontains=query)
results= BlogModel.objects.filter(lookups).distinct()
context={'results': results,
'submitbutton': submitbutton,
'dataset': dataset,
}
return render(request, 'layout/search-main.html', context)
else:
return render(request, 'layout/search-main.html')
else:
return render(request, 'layout/search-main.html')
class CatListView(ListView):
template_name = 'layout/category-main.html'
context_object_name = 'catlist'
def get_queryset(self):
content = {
'cat' : self.kwargs['category'],
'posts' :
BlogModel.objects.filter(category__name=self.kwargs['category']).filter(status='published')
}
return content
def category_list(request):
category_list = Category.objects.exclude(name='default')
context = {
"category_list": category_list,
}
return context
它说问题是由 blogapp.views.BlogDetailView 提出的,所以我将其添加到我的 BlogDetailView 将其渲染到我的主 html :
category_list = Category.objects.exclude(name='default')
context = {
"category_list": category_list,
}
我认为问题出在 link?我的导航栏或 BlogDetailView?,这是什么问题?
你必须接受它作为字符串
这样试试:
path('home/category/<str:category>/', views.CatListView.as_view(), name="category"),
案例已解决,我同时使用了 str 和 slug,所以我的 类别 与我的 _id 不匹配,需要匹配这两个
path('home/category/<slug:category>/', views.CatListView.as_view(), name="category"),
path('home/category/<slug:_id>/', views.BlogDetailView, name="blog")
问题:
Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/home/category/coding/ Raised by: blogapp.views.BlogDetailView
urls.py
from django.urls.conf import path
from blogapp.views import BlogDetailView, BlogListView,searchposts,CatListView
from . import views
app_name = "blogapp"
urlpatterns = [
path('', views.BlogListView, name="blogs"),
path('home/search/', views.searchposts, name= "searchposts"),
path('home/category/<slug:_id>/', views.BlogDetailView, name="blog"),
path('home/category/<category>/', views.CatListView.as_view(), name="category"),
]
如果我颠倒这两个
path('home/category/<slug:_id>/', views.BlogDetailView, name="blog"),
path('home/category/<category>/', views.CatListView.as_view(), name="category"),
显示类别的html,以及'path('home/category/slug:_id/',views.BlogDetailView的路径, name="blog"),' 未显示,反之亦然
这里是导航栏
的html {% for category in category_list %}
{% if forloop.counter < 4 %}
<li class="nav-item">
<a class="nav-link" href="home/category/{{category.name}}">{{ category.name|title }}</a>
</li>
{% endif %}
{% endfor %}
我要渲染的html
{% extends "_layouts/base.html" %}
{% block css %}
{{block.super}}
{% endblock css %}
{% block page_title %}Insert_your_name_here{% endblock %}
{% load static %} <!-- load the image -->
{% block maincontent %}
<!-- Banner -->
{% block banner %}
{% load static %}
{% include "layout/category_banner.html" %}
{% endblock %}
<!-- Banner Here -->
!-- This is the main template -->
<section class="blog-posts grid-system">
<div class="container">
<div class="row">
<div class="col-lg-8">
<div class="all-blog-posts">
<div class="row">
{% block categories-main %}
{% include "layout/category.html" %}
{% endblock %}
</div>
</div>
</div>
{% block sidebars %}
{% load static %}
{% include "layout/Sidebars.html" %}
{% endblock %}
</div>
</div>
</section>
{% endblock %}
这是我的views.py
def BlogDetailView(request,_id):
try:
category_list = Category.objects.exclude(name='default')
dataset = BlogModel.newmanager.all()
data = BlogModel.newmanager.get(slug =_id)
comments = CommentModel.objects.filter(blog = data, status=True)
except BlogModel.DoesNotExist:
raise Http404('Data does not exist')
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
Comment = CommentModel(your_name= form.cleaned_data['your_name'],
comment = form.cleaned_data['comment'],
blog=data)
Comment.save()
return redirect(f'/home/category/{_id}')
else:
form = CommentForm()
context = {
'data':data,
'form':form,
'comments':comments,
'dataset': dataset,
"category_list": category_list,
}
return render(request,'layout/post-details-main.html',context)
def searchposts(request):
dataset = BlogModel.newmanager.all()
if request.method == 'GET':
query= request.GET.get('q')
submitbutton= request.GET.get('submit')
if query is not None:
lookups= Q(blog_title__icontains=query) | Q(blog__icontains=query)
results= BlogModel.objects.filter(lookups).distinct()
context={'results': results,
'submitbutton': submitbutton,
'dataset': dataset,
}
return render(request, 'layout/search-main.html', context)
else:
return render(request, 'layout/search-main.html')
else:
return render(request, 'layout/search-main.html')
class CatListView(ListView):
template_name = 'layout/category-main.html'
context_object_name = 'catlist'
def get_queryset(self):
content = {
'cat' : self.kwargs['category'],
'posts' :
BlogModel.objects.filter(category__name=self.kwargs['category']).filter(status='published')
}
return content
def category_list(request):
category_list = Category.objects.exclude(name='default')
context = {
"category_list": category_list,
}
return context
它说问题是由 blogapp.views.BlogDetailView 提出的,所以我将其添加到我的 BlogDetailView 将其渲染到我的主 html :
category_list = Category.objects.exclude(name='default')
context = {
"category_list": category_list,
}
我认为问题出在 link?我的导航栏或 BlogDetailView?,这是什么问题?
你必须接受它作为字符串
这样试试:
path('home/category/<str:category>/', views.CatListView.as_view(), name="category"),
案例已解决,我同时使用了 str 和 slug,所以我的 类别 与我的 _id 不匹配,需要匹配这两个
path('home/category/<slug:category>/', views.CatListView.as_view(), name="category"),
path('home/category/<slug:_id>/', views.BlogDetailView, name="blog")