How can I include user profile's images/logos on django comments

≡放荡痞女 提交于 2019-12-04 13:50:29

Subclassing User is not recommended in Django. Instead, create a separate profile model.

Let's assume you have your app, foo.

In foo's models.py, add:

class FooUserProfile(models.Model):
   user = models.ForeignKey(User, unique=True)
   user_image = models.ImageField()

Then, in your settings.py, add:

AUTH_PROFILE_MODULE = 'foo.FooUserProfile'

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 }}"/>

A caveat: you will need to create a FooUserProfile and associate it with your User any time you create new User.

You can read more in the Django documentation, Storing additional information about users or in the article Django tips: extending the User model

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!