我已经构建了基本的 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>
我认为您的 all-posts.html 模板中的双引号存在问题。如果使用外部双引号,则内部双引号应该是单数。尝试改变
<link rel="stylesheet" href="{% static "blogapp/post.css" %}"/>
至
<link rel="stylesheet" href="{% static 'blogapp/post.css' %}"/>