我試圖在其個人檔案頁面上顯示用戶的所有信息。這包括他們的個人資料圖片,但是我無法得到要顯示的圖片。我認爲一切都是正確的,我已經找到了一些東西,我試圖分別匹配它,但是我顯然錯過了一些東西。這裏是我的代碼:Django:在DetailView模板中顯示數據庫中的圖像
型號:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from users.choices import *
# Create your models here.
class UserProfileInfo(models.Model):
user = models.OneToOneField(User)
join_date = models.DateTimeField(default=timezone.now)
profile_pic = models.ImageField(upload_to='profile_pics',blank=True)
location = models.CharField(max_length=150)
title = models.CharField(max_length=250)
user_type = models.IntegerField(choices=USER_TYPE_CHOICES,default=1)
website = models.URLField(max_length=100,blank=True)
about = models.TextField(max_length=500,default='about')
twitter = models.CharField(max_length=50,blank=True)
dribbble = models.CharField(max_length=50,blank=True)
github = models.CharField(max_length=50,blank=True)
def __str__(self):
return self.user.username
UserProfileView:
class UserProfileView(DetailView):
model = UserProfileInfo
配置文件模板(userprofileinfo_detail.html):
{% extends "base.html" %}
{% block content %}
<div class="sidebar-userinfo">
<img class="profile-pic" src="{{ userprofileinfo.profile_pic.url }}">
<h2>{{ userprofileinfo.username }}</h2>
<p class="accent">Score:</p>
<p>Score goes here</p>
<form>
<button type="submit">Follow</button>
</form>
<p class="accent">Title:</p>
<p class="profile-info">{{ userprofileinfo.title }}</p>
<p class="accent">Website:</p>
<p class="profile-info">{{ userprofileinfo.website }}</p>
<p class="accent">I'm a:</p>
{% if userprofileinfo.user_type == 1 %}
<p class="profile-info">Designer</p>
{% elif userprofileinfo.user_type == 2 %}
<p class="profile-info">Designer</p>
{% else %}
<p class="profile-info">Both</p>
{% endif %}
<p class="accent">About Me:</p>
<p class="profile-info">{{ userprofileinfo.about }}</p>
<p class="accent">Member Since:</p>
<p class="profile-info">{{ userprofileinfo.join_date }}</p>
<p class="accent">Location:</p>
<p class="profile-info">{{ userprofileinfo.location }}</p>
<p class="accent">Twitter:</p>
<p class="profile-info">{{ userprofileinfo.twitter }}</p>
<p class="accent">Dribbble:</p>
<p class="profile-info">{{ userprofileinfo.dribbble }}</p>
<p class="accent">Git Hub:</p>
<p class="profile-info">{{ userprofileinfo.github }}</p>
</div>
{% endblock %}
你見過在源鏈接其顯示的內容使用視圖頁面的源代碼。 –
你有什麼輸出? – Dharmesh
這是源代碼'/ media/profile_pics/garrett-pro-pic.jpg'的URL,它看起來是正確的url,輸出只是默認的「?」當它無法找到圖像的東西 – Garrett