Django不会在HTML中显示数据库中的图像

时间:2017-10-19 19:33:34

标签: python html mysql django

您好我正在尝试从与登录用户对应的Django MySQL数据库中显示用户个人资料图片。

这是我的settings.py

Descendant2

这是我的urls.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/'

这就是我在html文件中的内容:

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}),
]

这是我的表我正在尝试从以下位置检索图片: Database screenshot

我不知道如何让它显示,我认为我的网址映射是正确的,因为我可以查看图像,如果我去http://127.0.0.1:8000/media/profile_images/trump.jpg

有什么想法吗?对不起,我有点像诺贝尔。

1 个答案:

答案 0 :(得分:0)

FileField上传到媒体方面,而不是静态的,所以你只需要这样做;

<img src="{{ user.userprofile.picture.url }}" alt="" />

以下是此方案的示例; https://www.simplifiedpython.net/django-file-upload-tutorial/

相关问题