Django 不显示来自模型的图像
Django not displaying image from model
我无法让图像显示在我的 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,并且我可以在我的目录中看到正确的路径,其中包含图像。非常感谢任何帮助。
您还应该将以下内容添加到 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)
我无法让图像显示在我的 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,并且我可以在我的目录中看到正确的路径,其中包含图像。非常感谢任何帮助。
您还应该将以下内容添加到 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)