我正在做一个项目,我有一个问题。问题是django没有在模板中显示模型数据,我无法解决问题。这是型号.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
这是视图.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 %}
检查您的post对象是否为None
。试着打印。
正如我在你的代码中看到的,u把你的slug放为kwarg,实际上并没有关闭括号:
sno=models.AutoField(primary_key=True slug=models.CharField(max_length=130)
如果不是复制错误,可以这样做:
sno=models.AutoField(primary_key=True)
slug=models.SlugField(max_length=130)