我通过一对一的字段扩展了标准django用户模型。制作新闻块,并在那里添加评论。在评论中我无法从UserProfile模型显示用户头像,导致不明白如何正确地向数据库询问它D;。这是我的代码:
main / models.py
render() {
if(this.state.loading){
return null;
}else{
const [book={}, ...rest] = this.state.details;
console.log(JSON.stringify(book, null, 2));
return (
<div>
<h2>{book.title}</h2>
<p>{book.author.name}</p>
</div>
);
}
}
简档/ models.py
from django.db import models
from django.utils import timezone
from django.contrib import auth
from django.contrib.auth.forms import User
from django.shortcuts import render, redirect
from profiles.models import UserProfile
# Create your models here.
class News(models.Model):
news_title = models.CharField(max_length=250)
news_body = models.TextField(max_length=2000, blank=True)
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
image = models.FileField()
published_date = models.DateTimeField(blank=True, null=True)
def publish(self, request):
self.published_date = timezone.now()
self.save()
return redirect('index')
def __str__(self):
return self.news_title
class Comment(models.Model):
news = models.ForeignKey('main.News', related_name='comments',
on_delete=models.CASCADE)
author = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approved_comment = models.BooleanField(default=False)
def approve(self):
self.approved_comment = True
self.save()
def __str__(self):
return self.text
main / views.py(meme_detail是视图,应该是带有用户信息的评论)
class UserProfile(models.Model):
JEW_CHOICE = (
('Да', 'Да'),
('Нет', 'Нет'),
)
MF_CHOICE = (
('М', 'М'),
('Ж', 'Ж')
)
user = models.OneToOneField(User, on_delete=models.CASCADE)
country = models.CharField(max_length=100, default='', blank=True)
city = models.CharField(max_length=100, default='', blank=True)
description = models.CharField(max_length=500, default='', blank=True)
website = models.URLField(default='', blank=True)
avatar = models.ImageField(default='', blank=True)
gender = models.CharField(max_length=100, choices = MF_CHOICE, default = 'М', blank=True)
jew = models.CharField(max_length=100, choices = JEW_CHOICE, default = 'Да', blank=True)
def __str__(self):
return self.user.username
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.get_or_create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.userprofile.save()
@property
def avatar_url(self):
if self.avatar and hasattr(self.avatar, 'url'):
return self.avatar.url
meme_detail.html(带评论的新闻模板)
def meme_detail(request, pk):
news = get_object_or_404(News, pk=pk)
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.author = request.user
comment.news = news
comment.save()
return redirect('main:meme_detail', pk=news.pk)
else:
form = CommentForm()
return render(request, 'main/meme_detail.html', {'news': news, 'form': form,})
因此,在此模板中,“userprofile.avatar.url”应该是User avatar上的对象引用。我尝试了很多东西,但总是一样:not displaying
答案 0 :(得分:1)
你应该这样做:
<img src="{{ comment.author.userprofile.avatar.url }}" alt="user-avatar" width="100px" height="100px">
您的评论有一个用户(作者)的外键,而用户有一对一的字段到UserProfile,这是具有头像属性的字段。
另一个提示:
你不应该真正减少CSS中的图像(宽度:100px;高度:100px;),而是使用一个允许你创建图像缩略图的工具。我使用sorl-thumbnail并且不能推荐它。
原因是如果每个用户上传1000x1000图片,您就会下载那些您真正不需要的大图片,因此您的网站会变慢。
答案 1 :(得分:0)
也许你应该尝试访问模板中的User
对象,而不是Userprofile
。
<img src="{{ user.userprofile.avatar.url }}" ...