提问人:techiekamran 提问时间:10/21/2023 更新时间:10/21/2023 访问量:32
将“posts-details-page”反转,未找到参数“('',)”。尝试了 1 种模式: ['all_posts/(?P<slug>[-a-zA-Z0-9_]+)$']
Reverse for 'posts-details-page' with arguments '('',)' not found. 1 pattern(s) tried: ['all_posts/(?P<slug>[-a-zA-Z0-9_]+)$']
问:
我已经构建了基本的 Django 项目,但在尝试从项目中检索所有帖子时出现错误。您可以从 github 下载代码来创建问题,如果有人可以提供帮助,请指导我如何解决此问题。https://github.com/techiekamran/BlogProject.git 和分支名称为 starting-blog-project
urls.py
from django.urls import path
from blogapp import views
urlpatterns = [
path("",views.starting_page,name='starting-page'),
path("posts",views.posts,name="posts-page"),
path("all_posts/<slug:slug>",views.post_details,name='posts-details-page'),
#posts/my-first-post
]
views.py
from datetime import date
from django.shortcuts import render
def get_date(all_posts):
return all_posts['date']
def starting_page(request):
sorted_post = sorted(all_posts,key=get_date)
latest_post = sorted_post[-3:]
return render(request,'blogapp/index.html',{'posts':latest_post})
def posts(request):
return render(request,'blogapp/all-posts.html',{'all_posts':all_posts})
def post_details(request,slug):
identified_post=next(post for post in all_posts if post['slug']==slug)
return render(request,'blogapp/post-details.html',{'post':identified_post})
index.html
{% extends "_base.html" %}
{% load static %}
{% block title %}
Home
{% endblock %}
{% block css_files %}
<link rel="stylesheet" href="{% static 'blogapp/post.css' %}"/>
<link rel="stylesheet" href="{% static 'blogapp/index.css' %}"/>
{% endblock %}
{% block content %}
<section id="welcome">
<header>
<img src="{% static 'blogapp/images/max.png' %}" alt="Max= The author of this Blog"/>
<h2>Demo blog des</h2>
</header>
<p>Hi, this is the our first blog</p>
</section>
<section id="latest-posts">
<h2>My latest thoughts</h2>
<ul>
{% for post in posts %}
{% include 'blogapp/includes/post.html' %}
{% endfor %}
</ul>
</section>
<section id="about">
<h2>What I do </h2>
<p>I love programming</p>
<p>My goal is to keep</p>
</section>
{% endblock %}
post-details.html
{% extends "_base.html" %}
{% load static %}
{% block title %}
{{ post.title }}
<!--This Post Details-->
{% endblock %}
{% block css_files %}
<link rel="stylesheet" href="{% static "blogapp/post-details.css" %}"/>
{% endblock %}
{% block content %}
<section id="summary">
{{ post.title }}
<!--<h2>Post Title</h2>-->
<article>
<img src="{% static 'blogapp/images/mountains.jpg' %}" alt="Post Title"/>
<address>By Demo</address>
<div>
Last updated on <time>Oct 20</time>
</div>
</article>
</section>
<main>
{{ post.content }}
<!--<p>this is dumy data</p>
<p>this is dumy data</p>
<p>this is dumy data</p>
<p>this is dumy data</p>
-->
</main>
{% endblock %}
all-posts.html
{% extends "_base.html" %}
{% load static %}
{% block title %}
All my posts
{% endblock %}
{% block css_files %}
<link rel="stylesheet" href="{% static "blogapp/post.css" %}"/>
<link rel="stylesheet" href="{% static 'blogapp/all-post.css' %}"/>
{% endblock %}
{% block content %}
<section id="all-posts">
<h2>My collected posts</h2>
<ul>
{% for post in all_post %}
{% include 'blogapp/includes/post.html' %}
{% endfor %}
<!--
{% include 'blogapp/includes/post.html' %}
{% include 'blogapp/includes/post.html' %}
{% include 'blogapp/includes/post.html' %}
{% include 'blogapp/includes/post.html' %}
{% include 'blogapp/includes/post.html' %}
{% include 'blogapp/includes/post.html' %}
-->
</ul>
</section>
{% endblock %}
post.html
{% load static %}
<li>
<article class="post">
<a href="{% url 'posts-details-page' post.slug %}">Link</a>
<a href="{% url 'posts-details-page' post.slug %}">
<img src="{% static 'blogapp/images/'|add:post.image %}" alt="Mountain Hiking" />
<!--<img src="{% static 'blogapp/images/mountains.jpg' %}" alt="Mountain Hiking" />-->
<div class="post__content">
{{ post.slug }}
<h3>{{ post.title }}
<p>{{ post.content }}</p>
<!--
<h3>Mountain hiking</h3>
<p>There is nothing hike</p>
-->
</div>
</a>
</article>
</li>
答:
0赞
Vlad Marchenko
10/21/2023
#1
我认为您的所有帖子 .html 模板中的双引号存在问题。如果使用外部双引号,则内部双引号应为单数。尝试更改
<link rel="stylesheet" href="{% static "blogapp/post.css" %}"/>
自
<link rel="stylesheet" href="{% static 'blogapp/post.css' %}"/>
评论
0赞
techiekamran
10/21/2023
嗨弗拉德,我试图更改,但仍然相同的错误 在模板渲染期间出错 BlogProject\blogapp\templates\blogapp\includes\post.html,第 6 行错误 “posts-details-page”反转,参数“('',)' 未找到。尝试了 1 种模式: ['all_posts/(?P<slug>[-a-zA-Z0-9_]+)$'] 1 {% load static %} 2 3 <li> 4 5 <article class=“post”> 6 <a href=“{% url 'posts-details-page' post.slug %}”>Link</a> 错误在第 6 行
0赞
Vlad Marchenko
10/21/2023
好的,我认为添加蛞蝓的方式存在问题,请尝试:<a href="{% url 'posts-details-page' slug=post.slug %}">Link</a>
评论