【问题标题】:django.template.library.InvalidTemplateLibrary: Invalid template library specifieddjango.template.library.InvalidTemplateLibrary:指定的模板库无效
【发布时间】:2019-09-13 23:45:54
【问题描述】:

我正在尝试构建一个博客应用程序。 Ran makemigrations 和 migrate 并创建了超级用户。但是我在运行服务器时遇到了错误。

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)

【问题讨论】:

  • 请给出完整的回溯

标签: django django-models django-forms django-templates django-views


【解决方案1】:

你好像有

from blog.models import POST

blog/templatetags/blog_tags.py.

大写在 Python 变量名中很重要。应该是:

from blog.models import Post

【讨论】:

  • 是的,我写了 Post,因为我的模型类名称是 Post。但它会自动转换为 POST。
  • 对不起,我没有正确检查。太感谢了。非常感谢您的快速帮助
猜你喜欢
  • 2022-06-15
  • 2018-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-12
  • 1970-01-01
  • 2020-10-21
  • 1970-01-01
相关资源
最近更新 更多