Django:图像字段未显示
Django: Image Field not showing up
所以我的图片没有显示,这是我的代码:
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">
我做对了,我也可以在我的媒体文件夹中看到我的图像,但在我的模板中没有显示,
有人知道我的代码有什么问题吗?
由于 ForeignKey
中的 related_name='profile'
是 'profile'
,您可以使用
访问它
<img src="{{ user.<b>profile</b>.avatar.url }}" alt="profile image">
related_name=…
parameter [Django-doc] 是 reverse 中关系的名称。所以要从User
对象中获取相关的ProfileImage
对象。
您也可以决定将related_name=…
更改为related_name='profileimage'
,那么您当然会使用{{ user.profileimage.avatar.url }}
。
所以我的图片没有显示,这是我的代码: 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">
我做对了,我也可以在我的媒体文件夹中看到我的图像,但在我的模板中没有显示, 有人知道我的代码有什么问题吗?
由于 ForeignKey
中的 related_name='profile'
是 'profile'
,您可以使用
<img src="{{ user.<b>profile</b>.avatar.url }}" alt="profile image">
related_name=…
parameter [Django-doc] 是 reverse 中关系的名称。所以要从User
对象中获取相关的ProfileImage
对象。
您也可以决定将related_name=…
更改为related_name='profileimage'
,那么您当然会使用{{ user.profileimage.avatar.url }}
。