如何在django评论中包含用户个人资料的图片/标志 [英] How can I include user profile's images/logos on django comments
问题描述
我已经在django-users上发布,但没有收到回复!
I've posted this on django-users but haven't received a reply!
所以我有自己的个人资料对象其中一个字段是imagefield,(显然)用于用户上传他们的标志/缩略图。
So I have my own profile objects for users (subclass of User). One of the fields is imagefield, which is (obviously) used for users to upload their logo/thumbnail.
问题是我如何将他们的意见包括在内?
The question is how I can include this on their comments?
提前致谢!
推荐答案
子类化Django中不推荐用户。而是创建一个单独的配置文件模型。
Subclassing User is not recommended in Django. Instead, create a separate profile model.
让我们假设您有您的应用程式, 。
Let's assume you have your app, foo.
> models.py ,请添加:
In foo's models.py, add:
class FooUserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
user_image = models.ImageField()
,在 settings.py 中添加:
AUTH_PROFILE_MODULE = 'foo.FooUserProfile'
现在,只要有一个用户对象,就可以使用get_profile()函数获得它的profile对象。在您的情况下,您可以在模板中添加:
Now, whenever you have a user object, you can get its profile object with the get_profile() function. In your case, in the template you could add:
<img src="{{ comment.user.get_profile.user_image }}"/>
注意:您需要创建一个FooUserProfile,
A caveat: you will need to create a FooUserProfile and associate it with your User any time you create new User.
您可以在Django文档中阅读更多内容,存储有关用户的其他信息或在文章 Django提示:扩展用户模型
You can read more in the Django documentation, Storing additional information about users or in the article Django tips: extending the User model
这篇关于如何在django评论中包含用户个人资料的图片/标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!