Python 德扬戈元';在HTML中显示来自数据库的图像
您好,我正在尝试从Django MySQL数据库中获取要显示的用户配置文件图像,该数据库对应于登录的用户 这是我的设置.pyPython 德扬戈元';在HTML中显示来自数据库的图像,python,html,mysql,django,Python,Html,Mysql,Django,您好,我正在尝试从Django MySQL数据库中获取要显示的用户配置文件图像,该数据库对应于登录的用户 这是我的设置.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,'templa
# 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/'
这是我的URL.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}),
]
这是我试图从中检索图片的表格:
我不知道如何让它显示,我认为我的URL映射是正确的,因为如果我转到
有什么想法吗?对不起,我有点无所事事。
FileField
上传到媒体端的东西,不是静态的,所以你只需要做
<img src="{{ user.userprofile.picture.url }}" alt="" />
下面是这个场景的一个例子 谢谢你,成功了!我试图用这些%来做,但没有用。。你的用了双括号
<img src="{{ user.userprofile.picture.url }}" alt="" />