我正在尝试构建Blog应用程序。进行makemigrations和迁移,还创建了超级用户。但是我在运行服务器时遇到错误。
django.template.library.InvalidTemplateLibrary:指定了无效的模板库。尝试加载“ blog.templatetags.blog_tags”时引发ImportError:无法从“ blog.models”导入名称“ POST”
请帮助我。... 我的models.py文件是
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from django.urls import reverse
# Create your models here.
class CustomManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status='published')
from taggit.managers import TaggableManager
class Post(models.Model):
STATUS_CHOICES=(('draft','Draft'),('published','Published'))
title=models.CharField(max_length=256)
slug=models.SlugField(max_length=264,unique_for_date='publish')
author=models.ForeignKey(User,related_name='blog_posts',on_delete=models.DO_NOTHING)
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')
objects=CustomManager()
tags=TaggableManager()
class Meta:
ordering=('-publish',)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post_detail',args=[self.publish.year,self.publish.strftime('%m'),self.publish.strftime('%d'),self.slug])
class Comment(models.Model):
post=models.ForeignKey(Post,related_name='comments',on_delete=models.DO_NOTHING)
name=models.CharField(max_length=40)
email=models.EmailField()
body=models.TextField()
created=models.DateTimeField(auto_now_add=True)
updated=models.DateTimeField(auto_now=True)
active=models.BooleanField(default=True)
class Meta:
ordering=('-created',)
def __str__(self):
return 'Commented by {} on {}'.form(self.name,self.post)
答案 0 :(得分:0)
看起来像你
from blog.models import POST
在blog/templatetags/blog_tags.py
中。
大写在Python变量名称中很重要。应该是:
from blog.models import Post