django - 如何在 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 %}
解决方案
检查天气您的帖子对象是None
。尝试打印它。
推荐阅读
- php - 不在,在声明中不起作用(预订)
- c# - 如何通过 c# 为 Web API 调用者调用 signalR 函数
- c# - VS2017 + Resharper 如何在输入打开引号时用引号包围选择?
- azure - Web 应用程序未验证自定义域名
- android - 颤振路线。移动应用程序开发
- spring-boot - Spring Data MongoRepository 保存导致重复键错误
- angularjs - 我如何在angularjs中从aws s3压缩和下载完整文件夹
- javascript - 如何一键切换音频播放/暂停
- java - [无法将类型“java.lang.String”的属性值转换为所需类型“java.sql.Time”
- python - Pandas-Python 日期范围