Django:在DetailView模板

时间:2017-09-05 17:05:41

标签: python django

我正在尝试在其个人资料页面上显示所有用户的信息。这包括他们的个人资料图片,但我无法显示图像。我认为一切都是正确的,我已经找到了一些东西,我试图分别匹配它,但我显然遗漏了一些东西。这是我的代码:

型号:

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 %}

1 个答案:

答案 0 :(得分:0)

如果您处于调试模式,则需要在结尾处将以下内容添加到urls.py:

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

在调试模式下提供媒体文件