Django:在DetailView temp中显示数据库中的图像

2024-04-25 20:27:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在他们的个人资料页面上显示所有的用户信息。这包括他们的个人资料图片,但是我不能让图像显示。我觉得一切都是对的,我在这上面找到了一些东西,我试着把它们分别对应起来,但是我显然遗漏了一些东西。这是我的代码:

型号:

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

用户配置文件视图:

^{pr2}$

配置文件模板(userprofileinfo_详细信息.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 %}

Tags: fromimportinfotruemodelstypeprofilelength
1条回答
网友
1楼 · 发布于 2024-04-25 20:27:04

如果处于调试模式,则需要将以下内容添加到网址.py最后:

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

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

相关问题 更多 >