[英]Django won't display image from database in HTML
嗨,我正在嘗試從與登錄的用戶相對應的 Django MySQL 數據庫中獲取要顯示的用戶個人資料圖像。
這是我的settings.py
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')
STATIC_DIR = os.path.join(BASE_DIR,'static')
MEDIA_DIR = os.path.join(BASE_DIR,'media')
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR, ]
# Media files
MEDIA_ROOT = MEDIA_DIR
#MEDIA_DIRS = [MEDIA_DIR, ]
MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/admin/media/'
這是我的 urls.py:
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^smartcity/',include('smartcity.urls')),
#url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register'),
#url(r'^accounts/register/$', views.register, name='registration'),
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^admin/', admin.site.urls),
url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
]
這就是我在 html 文件中的內容:
src="{% static 'user.userprofile.picture.url' %}"
這是我試圖從中檢索圖片的表格:數據庫截圖
我不知道如何讓它顯示,我認為我的 URL 映射是正確的,因為如果我去http://127.0.0.1:8000/media/profile_images/trump.jpg我可以查看圖像
有任何想法嗎? 對不起,我有點菜鳥。
FileField
上傳到媒體端的東西,不是靜態的,所以你只需要這樣做;
<img src="{{ user.userprofile.picture.url }}" alt="" />
這是這種情況的一個例子; https://www.simplifiedpython.net/django-file-upload-tutorial/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.