首页 > 解决方案 > 如何在 django 模板中显示模型数据?

问题描述

我正在做项目,但我遇到了问题。问题是 django 没有在模板中显示模型数据,我无法解决问题。这是models.py

 from django.db import models
 from django.contrib.auth.models 
 import User
 from django.utils.timezone 
 import now

class videopost(models.Model):

 sno=models.AutoField(primary_key=True)  slug=models.CharField(max_length=130)
title=models.CharField(max_length=255)
image = models.ImageField(upload_to='staticfiles/images/')
content=models.TextField()
timeStamp=models.DateTimeField(blank=True)


   def__str__(self):
        return self.title

这是views.py

def videoposts(request,slug):

    post=videopost.objects.filter(slug=slug).first()
    context={'post':post}

    return render(request,'videos/videopost.html',context)

这是模板

{% extends 'base.html' %}
{% load static %}
{% load embed_video_tags %}
{% block title %}How to run python in mobile?{% endblock title %}
{% block basecss%}"{% static '/css/home.css' %}" {% endblock basecss %}
{% block extracss %}"{% static '/css/videos.css' %}" {% endblock extracss %}


{% block classvideos%} current {% endblock classvideos %}
{% block body %}
            
{{post.content}}            
{{post.title}}
{% endblock body %}

标签: djangodjango-modelsdjango-templates

解决方案


检查天气您的帖子对象是None。尝试打印它。


推荐阅读