django - AttributeError - 'unicode' 对象没有属性 'META'
django - AttributeError - 'unicode' object has no attribute 'META'
我正在使用信号和内容类型为用户创建 activity 流。活动由 post_save 信号创建。但是,我在模板中显示这些活动时遇到问题。
我收到此错误:
AttributeError at /user/user_activities/
'unicode' object has no attribute 'META'
显示为错误的行是这一行:
{{ activity.get_rendered_html }}
我不知道为什么会出现此错误。如果您能告诉我如何解决这个问题,我将不胜感激。谢谢。
这是 activity 的模型:
class Activity(models.Model):
actor = models.ForeignKey(User)
action = models.CharField(max_length=100)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
pub_date = models.DateTimeField(auto_now_add=True, auto_now=False)
class Meta:
verbose_name = 'Activity'
verbose_name_plural = 'Activities'
ordering = ['-pub_date']
def __unicode__(self):
return ("%s %s") % (self.actor.username, self.action)
def get_rendered_html(self):
template_name = '%s_activity.html' %(self.content_type.name)
return render(template_name, {
'object':self.content_object,
'actor':self.actor,
'action':self.action,
})
get_rendered_html()
的模板:
<div class="user_activity">
<p>{{ actor.username }} {{ action }} {{ object.user }}'s status</p>
<p>{{ object.body }}</p>
{% if object.image %}
<div class="activity_img_wrapper">
<p><img src="/media/{{ object.image }}"/></p>
</div>
{% endif %}
</div>
并且在观点中:
def user_activities(request, user_name):
user = User.objects.get(username=unquote(user_name))
activities = Activity.objects.filter(actor=user)
return render(request,'user_activites.html', {
'activities_of':user,
'activities':activities
})
最后在模板中:
{% block content %}
<div id="activities_wrapper">
{% if activities.count > 0 %}
{% for activity in activities %}
<div class="activity">
{{ activity.get_rendered_html }}
</div>
{% endfor %}
{% else %}
<div class="activity">
<p>No activity</p>
</div>
{% endif %}
</div>
{% endblock %}
错误在Activity.get_rendered_html()
方法中。
django.shortcuts.render
期望 request
对象作为第一个参数。您应该使用 render_to_string
而不是 render
:
from django.template.loader import render_to_string
def get_rendered_html(self):
template_name = '%s_activity.html' %(self.content_type.name)
return render_to_string(template_name, {
'object':self.content_object,
'actor':self.actor,
'action':self.action,
})
我正在使用信号和内容类型为用户创建 activity 流。活动由 post_save 信号创建。但是,我在模板中显示这些活动时遇到问题。
我收到此错误:
AttributeError at /user/user_activities/
'unicode' object has no attribute 'META'
显示为错误的行是这一行:
{{ activity.get_rendered_html }}
我不知道为什么会出现此错误。如果您能告诉我如何解决这个问题,我将不胜感激。谢谢。
这是 activity 的模型:
class Activity(models.Model):
actor = models.ForeignKey(User)
action = models.CharField(max_length=100)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
pub_date = models.DateTimeField(auto_now_add=True, auto_now=False)
class Meta:
verbose_name = 'Activity'
verbose_name_plural = 'Activities'
ordering = ['-pub_date']
def __unicode__(self):
return ("%s %s") % (self.actor.username, self.action)
def get_rendered_html(self):
template_name = '%s_activity.html' %(self.content_type.name)
return render(template_name, {
'object':self.content_object,
'actor':self.actor,
'action':self.action,
})
get_rendered_html()
的模板:
<div class="user_activity">
<p>{{ actor.username }} {{ action }} {{ object.user }}'s status</p>
<p>{{ object.body }}</p>
{% if object.image %}
<div class="activity_img_wrapper">
<p><img src="/media/{{ object.image }}"/></p>
</div>
{% endif %}
</div>
并且在观点中:
def user_activities(request, user_name):
user = User.objects.get(username=unquote(user_name))
activities = Activity.objects.filter(actor=user)
return render(request,'user_activites.html', {
'activities_of':user,
'activities':activities
})
最后在模板中:
{% block content %}
<div id="activities_wrapper">
{% if activities.count > 0 %}
{% for activity in activities %}
<div class="activity">
{{ activity.get_rendered_html }}
</div>
{% endfor %}
{% else %}
<div class="activity">
<p>No activity</p>
</div>
{% endif %}
</div>
{% endblock %}
错误在Activity.get_rendered_html()
方法中。
django.shortcuts.render
期望 request
对象作为第一个参数。您应该使用 render_to_string
而不是 render
:
from django.template.loader import render_to_string
def get_rendered_html(self):
template_name = '%s_activity.html' %(self.content_type.name)
return render_to_string(template_name, {
'object':self.content_object,
'actor':self.actor,
'action':self.action,
})