gpt4 book ai didi

python - 渲染时捕获 AttributeError : 'unicode' object has no attribute '_default_manager'

转载 作者:太空宇宙 更新时间:2023-11-03 19:23:15 24 4
gpt4 key购买 nike

按照实用 Django 教程出现此错误。在这里和谷歌上搜索过,没有发现任何提及这一点的内容,所以我想这将是我很容易错过的事情。异常(exception)情况在 gambino_tags.py 中:

context[self.varname] = self.model._default_manager.all()[:self.num]

模型.py

class Entry(models.Model):
# Entry types
LIVE_STATUS = 1
DRAFT_STATUS = 2
HIDDEN_STATUS = 3
STATUS_CHOICES = (
(LIVE_STATUS, 'Live'),
(DRAFT_STATUS, 'Draft'),
(HIDDEN_STATUS, 'Hidden'),
)
# Foreign Key
author = models.ForeignKey(User)
title = models.CharField(max_length=250, help_text='Maximum 250 characters.')
# Bit of redundancy with excerpt & excerpt_html and body & body_html, but seems to be cleanest way to seperate plain text and html
excerpt = models.TextField(blank=True, help_text='Add an excerpt - a short summary of your post.')
excerpt_html = models.TextField(editable=False, blank=True)
body = models.TextField(help_text='Add the content of your post.')
body_html = models.TextField(editable=False, blank=True)
pub_date = models.DateTimeField(default=datetime.datetime.now)
# Slug prepopulated from title in admin.py
slug = models.SlugField(unique_for_date='pub_date', help_text="Suggested value automatically generated from title. Must be unique.")
enable_comments = models.BooleanField(default=True)
featured = models.BooleanField(default=False, help_text="Make this post featured. Will provide additional sorting.")
status = models.IntegerField(choices=STATUS_CHOICES, default=LIVE_STATUS)
categories = models.ManyToManyField(Category)
tags = TagField()
# Adds objects and live override from LiveEntryManager class above
live = LiveEntryManager()
objects = models.Manager()

gambino_tags.py

from django import template
from django.db.models import get_model

def do_latest_content(parser, token):
bits = token.contents.split()
# Checks to make sure that there are 5 words in the tag
if len(bits) != 5:
raise template.TemplateSyntaxError("'get_latest_content' tag takes exactly four arguments")
model_args = bits[1].split('.')
# Checks to make sure first argument is appname.model name
if len(model_args) != 2:
raise template.TemplateSyntaxError("First argument to 'get_latest_content' must be an 'application name'.'model name' string")
model = get_model(*model_args)
# Checks to make sure model is != none
if model is None:
raise template.TemplateSyntaxError("'get_latest_content' tag got an invalid model: %s" % bits[1])
return LatestContentNode(bits[1], bits[2], bits[4])

class LatestContentNode(template.Node):
def __init__(self, model, num, varname):
self.model = model
# convert num to int
self.num = int(num)
self.varname = varname

def render(self, context):
# uses default manager for Entry.live.all() instances
context[self.varname] = self.model._default_manager.all()[:self.num]
return ''

register = template.Library()
register.tag('get_latest_content', do_latest_content)

base.html

        {% load gambino_tags %}
<h2>Five latest entries:</h2>
<ul>
{% get_latest_content gambino.entry 5 as latest_entries %}
{% for entry in latest_entries %}
<li>
<a class="list" href="{{ entry.get_absolute_url }}">{{ entry.title }}</a>
Posted {{ entry.pub_date|timesince }} ago.
</li>
{% endfor %}
</ul>
<h2>Five latest links:</h2>
<ul>
{% get_latest_content gambino.link 5 as latest_links %}
{% for link in latest_links %}
<li>
<a href="{{ link.get_absolute_url }}">{{ link.title }}</a>
Posted {{ link.pub_date|timesince }} ago.
</li>
{% endfor %}
</ul>

谢谢!

最佳答案

Node__init__model 参数是一个字符串(或者至少,这就是您传入的内容),不是 Model 对象。

bits = token.contents.split()
...
return LatestContentNode(bits[1], bits[2], bits[4])
...
class LatestContentNode(template.Node):
def __init__(self, model, num, varname):
self.model = model
...
context[self.varname] = self.model._default_manager.all()[:self.num]

也许你实际上的意思是......

return LatestContentNode(model, bits[2], bits[4])

关于python - 渲染时捕获 AttributeError : 'unicode' object has no attribute '_default_manager' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9090755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com