【问题标题】:Data is not fetching from database in python django数据未从 python django 中的数据库中获取
【发布时间】:2021-09-20 12:29:29
【问题描述】:

我在数据库中保存了一些帖子,我正在获取帖子以显示在 list.html 中。但数据未显示。

主 Urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls',namespace='blog')),
]

应用 url.py

from django.urls import path
from . import views

app_name = 'blog'

urlpatterns = [
    path('',views.post_list,name='post_list'),
    
path('<int:year>/<int:month>/<int:day>/<slug:post>/',views.post_detail,name='post_detail'),
]

Views.py

from django.core import paginator
from .models import Post
from django.shortcuts import render, get_list_or_404
def post_list(req):
    posts = Post.published.all()
    return render(req,'blog/post/list.html',{'posts':posts})

def post_detail(req, year,month,day,post):
    post = get_list_or_404(Post,slug=post,status='published',publish__year=year,publish__month=month,publish__day=day)
    return render(req,'blog/post/detail.html',
    {'post':post})

list.html

<h1>MyBlog</h1>
{% for post in posts %}
<h2>
    <a href="{{post.get_absolute_url}}">a{{post.title}}</a></h2>
<p class="date">
    Published{{post.Publish}} by {{post.author}}
</p>
{{post.body|truncatewords:30|linebreaks}}
{% endfor %}

模型.py

from django.db import models
from django.urls import reverse
from django.utils import timezone
from django.contrib.auth.models import User

class PublishedManager(models.Manager):
    def get_queryset(self) :
        return super(PublishedManager,self).get_queryset()\
            .filter(status='published')

class Post(models.Model):
    
    STATUS_CHOICES = (('draft', 'Draft'),('published','Published'),)
    title=models.CharField(max_length=250)
    slug=models.SlugField(max_length=250,unique_for_date='publish')
    author = models.ForeignKey(User,on_delete=models.CASCADE,related_name='blog_posts')
    body = models.TextField()
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10, choices=STATUS_CHOICES,default='draft')
    object = models.Manager()
    published = PublishedManager()
        

class Meta:
    ordering = ('-publish',)
def __str__(self) :
    return self.title

def get_absolute_url(self):
    return reverse("blog:post_detail", args=[self.publish.year,self.publish.month,self.publish.day,self.slug])

【问题讨论】:

  • Post.published.all() 是什么?通常是Post.objects.all()。你能在你的数据库中显示数据并在你的经理中显示方法published吗?
  • 嗨@yedpodtrzitko 我已经添加了models.py,请检查。
  • 好的,请阅读我提出的问题的另一部分。还有object = models.Manager() 是干什么用的?
  • 您到底看到了什么?您是什么都没看到,还是只是缺少一些数据?仔细检查您的查询实际返回的结果。
  • @yedpodtrzitko 嵌入图像显示错误。让我告诉你我在数据库中有两个帖子 - 新标题,还有一个帖子。每个都有表字段标题,slug,作者,发布,状态,

标签: python django database fetch


【解决方案1】:

通常是 Post.objects.all()。

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 2014-05-17
    • 2021-09-18
    • 2020-10-31
    • 2023-04-09
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多