我无法使图像显示在我的HTML模板中。现在,它仅显示损坏的图像。
urls.py
from django.conf.urls import url, include
from django.contrib import admin
from home import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^$', views.index),
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('accounts.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
models.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
description = models.CharField(max_length=100, default='')
city = models.CharField(max_length=100, default='')
website = models.URLField(default='')
phone = models.IntegerField(default=0)
profile_picture = models.ImageField(upload_to='profile_pics', blank=True)
profile.html
<div class="container">
<br>
<h2>{{ user }}</h2>
<br>
<p>Name: {{ user.first_name }} {{ user.last_name }}</p>
<img src="{{ user.userprofile.profile_picture.url }}">
<p></p>
<p>About Me: {{ user.userprofile.description }}</p>
<p>Phone Number: {{ user.userprofile.phone }}</p>
<a href="{% url 'edit_profile' %}">Edit Profile</a><br>
<!-- if profile is updated succesfully -->
{% if messages %}
{% for message in messages %}
<br><br>{{ message }}
{% endfor %}
{% endif %}
</div>
输出:
我不确定我在做什么错。我相信我已经正确设置了medial_url / media_root,并且可以在其中包含图像的目录中看到正确的路径。任何帮助将不胜感激。
答案 0 :(得分:0)
您还应该将以下内容添加到urls.py
,
urlpatterns = [
url(r'^$', views.index),
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('accounts.urls')),
]
if settings.DEBUG == True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)