所以我的图像没有显示,这是我的代码: models.py:
from django.contrib.auth.models import User
from django.db import models
from django.conf import settings
from django.utils.translation import gettext_lazy as _
class ProfileImage(models.Model):
"""
Profile model
"""
user = models.OneToOneField(
verbose_name=_('User'),
to=settings.AUTH_USER_MODEL,
related_name='profile',
on_delete=models.CASCADE
)
avatar = models.ImageField(upload_to='profile_image')
settings.py:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'mifamiliaesundesastre/media')
urls.py:
from django.contrib import admin
from django.urls import path, include
from Home.views import home_view
from accounts.views import login_view, register_view, logout_view, user_home
from chat.views import Messages
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_view, name='home'),
path('login/', login_view),
path('signup/', register_view),
path('logout/', logout_view),
path('home/', user_home),
path('messages/', include('chat.urls')),
path('messages/', Messages),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
user_home.html:
<img src="{{ user.profileimage.avatar.url }}" alt="profile image">
我做了所有正确的操作,也可以在媒体文件夹中看到我的图像,但是我的模板中没有显示, 有人知道我的代码有什么问题吗?
答案 0 :(得分:1)
在for(p in 1:3) {
x <- lapply(list, function(l) list(get(paste0("x",l))))
}
中将related_name='profile'
设为ForeignKey
,您可以使用
'profile'
related_name=…
parameter [Django-doc]是 reverse 中关系的名称。因此,要从<img src="{{ user.profile.avatar.url }}" alt="profile image">
对象获得相关的ProfileImage
对象。
您还可以决定将User
更改为related_name=…
,然后当然使用related_name='profileimage'
。