简体   繁体   English

Python 速成班 19-1 项目烦恼

[英]Python Crash Course 19-1 project troubles

19-1. 19-1。 Blog: Start a new Django project called Blog.博客:启动一个名为 Blog 的新 Django 项目。 Create an app called blogs in the project and a model called BlogPost.在项目中创建一个名为 blogs 的应用程序和一个名为 BlogPost 的 model。 The model should have fields like title, text, and date_added. model 应具有标题、文本和添加日期等字段。 Create a superuser for the project, and use the admin site to make a couple of short posts.为项目创建一个超级用户,并使用管理站点发布一些简短的帖子。 Make a home page that shows all posts in chronological order.制作一个按时间顺序显示所有帖子的主页。 Create a form for making new posts and another for editing existing posts.创建一个用于制作新帖子的表单和另一个用于编辑现有帖子的表单。 Fill in your forms to make sure they work.填写您的 forms 以确保它们正常工作。

I'm having issues trying to show the blog post title as well as the paired body of text underneath it.我在尝试显示博客文章标题及其下方的配对文本时遇到问题。 My current setup is showing the correct titles in order, but it's only showing the last entry for each bullet.我当前的设置按顺序显示正确的标题,但它只显示每个项目符号的最后一个条目。

models.py模型.py

from django.db import models

# Create your models here.
class Blog_post(models.Model):
    """ Create new blog post """
    title = models.CharField(max_length=60)
    date = models.DateTimeField(auto_now_add=True)
    body = models.TextField()
    
    def __str__(self):
        """ Return string representation of the title"""
        return self.title

urls.py网址.py

"""  Define URL patterns for blog_app """
from django.urls import path
from . import views

app_name = 'blog_app'
urlpatterns = [
    # Home page
    path('', views.index, name='index'),     
]

views.py视图.py

from django.shortcuts import render
from .models import Blog_post

# Create your views here.
def index(request):
    """ Home page for blog """
    posts = Blog_post.objects.order_by('date')
    #entry = posts.objects.get('body')
    for i in posts:
        entry = i.body
    context = {'posts': posts, 'entry': entry}
    return render(request, 'blog_app/index.html', context)

index.html索引.html

<p> Welcome to my blog page!</p>

{% block content %}
 <p> posts: </p>
 
  <ul>
   {% for post in posts  %}
     <li>
      {{ post }}
      {{ entry|linebreaks }}
     </li>
   {% empty %}
    <li> No Topics have been added yet.</li>
   {% endfor %}
  </ul>
   
{% endblock content %}

The issue is that you are overwriting entry in a for loop.问题是您正在覆盖 for 循环中的entry In your views.py :在您的views.py中:

    for i in posts:
        entry = i.body
    context = {'posts': posts, 'entry': entry}
    return render(request, 'blog_app/index.html', context)

After the for loop executes, entry will contain the last i.body .在 for 循环执行后, entry将包含最后一个i.body

You could do something like this in your template:您可以在模板中执行以下操作:

{% for post in posts  %}
     <li>
      {{ post.title }}
      {{ post.body|linebreaks }}
     </li>
   {% empty %}
    <li> No Topics have been added yet.</li>
   {% endfor %}

and you can then remove the entry context variable from views.py .然后您可以从views.py中删除entry上下文变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM