未显示默认个人资料照片

问题描述

我正在开发一个 twitter 克隆应用程序,我希望用户注册登录时拥有认的个人资料照片。我已将模型设置为上传认图像,但由于某种原因未显示

models.py

from django.db import models
from django.contrib.auth.models import User
#import pylibjpeg_libjpeg
from PIL import Image

# Create your models here.
class Profile(models.Model):
    user = models.OnetoOneField(User,on_delete=models.CASCADE)
    image = models.ImageField(upload_to='profile_pics',default='default.png')

    def __str__(self):
        return f'{self.user.username} Profile'

    @property
    def followers(self):
        return Follow.objects.filter(follow_user=self.user).count()

    @property
    def following(self):
        return Follow.objects.filter(user=self.user).count()
    def save(self,force_insert=False,force_update=False,using=None,update_fields=None):
        super().save()
        img = Image.open(self.image.path)
        if img.height > 300 or img.width > 300:
            output_size = (300,300)
            img.thumbnail(output_size)
            img.save(self.image.path)

views.py

@login_required 
def profile(request):
    if request.method == 'POST':
        uform = UserUpdateForm(request.POST,instance=request.user)
        pform = ProfileUpdateForm(request.POST,request.FILES,instance=request.user.profile)

        if uform.is_valid() and pform.is_valid():
            uform.save()
           pform.save()
            messages.success(request,f'Account updated.')
            return redirect('profile')
    else:
        uform = UserUpdateForm(instance=request.user)
        pform = ProfileUpdateForm(instance=request.user.profile)
    return render(request,'Users/profile.html',{'uform': uform,'pform': pform})

设置文件

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,"staticfiles")

MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'

html 文件

                <article class="media content-section" style="height:140px; width:100%">
                {% if user_profile != None %}
                <div class="mt-2 d-flex full-width align-items-center">
                    <img class="rounded-circle mr-3 img-fluid" style="width:90px; height:90px" src="{{ user_profile.profile.image.url }}">
                    <div>
                        <h4 style="text-align: left" class="white-important">
                            {{ user_profile.username}}
                        </h4>
                        <h6 style="text-align:left" class="email-profile">
                            {{ user.email }}
                        </h6>
                    </div>
                </div>
                {% else %}
                <div class="mt-2 d-flex full-width align-items-center">
                    <img class="rounded-circle mr-3" style="width:90px; height:90px;" src="{{ user.profile.image.url }}" alt="profile picture">
                    <div>
                        <h4 style="text-align: left" class="white-important">
                            {{ user.username }}
                        </h4>
                        <h6 style="text-align:left" class="email-profile">
                            {{ user.email }}
                        </h6>
                    </div>
                </div>
                {% endif %}

我项目的 urls.py 文件

from django.contrib import admin
from django.urls import path,include
from django.contrib.auth import views as auth_views
from django.conf import settings
from django.conf.urls.static import static
from Users import views as users_views

urlpatterns = [
    path('admin/',admin.site.urls),path('login/',auth_views.LoginView.as_view(template_name='Users/login.html'),name='login'),path('logout/',auth_views.logoutView.as_view(template_name='Users/logout.html'),name='logout'),path('password-reset/',auth_views.PasswordResetView.as_view(template_name='Users/password_reset.html'),name='password-reset'),path('password-reset/done',auth_views.PasswordResetDoneView.as_view(template_name='Users/password_reset_done.html'),name='password-reset-done'),path('password-reset/confirm/<uid64>/<token>/',auth_views.PasswordResetConfirmView.as_view(template_name='Users/password_reset_confirm.html'),path('register/',users_views.register,name='register-users'),path('profile/',users_views.profile,name='profile'),path('search/',users_views.SearchView,name='search'),path('',include("Blog.urls")),]


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

这是用户登录后的输出截图

enter image description here

解决方法

您似乎没有在本地提供媒体文件。检查文档 here

,

在浏览器上检查图像并查看正在呈现的内容。另外,尝试在 media_root 中添加一个斜杠,使其为 MEDIA_ROOT = os.path.join(BASE_DIR,'media/')

,

在 views.py 中检查 pform.save() 的缩进 ...

如果仍然无法正常工作,请按照/检查这些步骤:

settings.py:

STATIC_DIR = os.path.join(BASE_DIR,"static")
MEDIA_DIR = os.path.join(BASE_DIR,'media')


# Static files (CSS,JavaScript,Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    STATIC_DIR,]

#MEDIA
MEDIA_ROOT = MEDIA_DIR
MEDIA_URL = '/media/' 


LOGIN_URL = 'user_login'

在您的基本目录(您的 manage.py 文件所在的文件夹)中创建一个名为 media 的文件夹,其中包含一个子文件夹 profile_pics

也检查这个类似的代码:

models.py:

from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class user_profile(models.Model):

    #using default User model by linking 
    user = models.OneToOneField(User,on_delete=models.CASCADE)

    #additional fields
    website = models.URLField(blank=True)
    profile_picture = models.ImageField(upload_to='profile_pictures',blank = True )
    bio = models.CharField(blank=True,max_length=300)

    def __str__(self):
        return self.user.username

forms.py:

from django import forms
from django.contrib.auth.models import User
from .models import user_profile

class User_form(forms.ModelForm):

    password = forms.CharField(widget=forms.PasswordInput())
    
    class Meta:
        model = User
        fields = ("username","email","password")

class user_profile_form(forms.ModelForm):
    
    class Meta:
        model = user_profile
        fields = ("profile_picture","website","bio")

views.py:

def signup(request):
    registered = False

    if request.method == "POST":
        
        user_form = User_form(data=request.POST)
        user_profileform = user_profile_form(data=request.POST)

        if(user_form.is_valid() and user_profileform.is_valid()):
            user = user_form.save()
            user.set_password(user.password)
            user.save()

            profile = user_profileform.save(commit=False)
            profile.user = user

                    
            if 'profile_picture' in request.FILES:
                profile.profile_picture = request.FILES['profile_picture']
            
            profile.save()

            registered = True
        
        else:
            print(user_form.errors,user_profileform.errors)
    
    else:
        user_form = User_form()
        user_profileform = user_profile_form()

    return render(request,"registration/signup.html",{'user_form': user_form,'user_profileform' : user_profileform,'registered' : registered  } )

还要确保您的模板表单具有“multipart/form-data”

signup.html:

<form enctype="multipart/form-data" method="POST">
                    <div class="container sign-form">
                        {% csrf_token %}
                        
                        {{ user_form |as_bootstrap  }}
                        {{ user_profileform |as_bootstrap  }}

                    
                        <!-- {{ user_profileform.as_p }} -->
                        <input type="submit" class = “btn btn-default” name="submit" value="submit">

还要检查您是否正确渲染了模板中的图像。像这样,

<div class="container">
                <img src="media/{{[context_object_name].image}}" class="imgbar"  id="img">
        </div>

现在还没解决,打开浏览器控制台看看显示的错误是什么

编辑:

将此[+ static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)] 添加到您的 urls.py,

urls.py:

from django.urls import path,include
from . import views
from django.conf import settings
from django.conf.urls.static import static

app_name = 'homepage'
urlpatterns = [
    path('',views.home,name = "home"),path('contact',views.contact,name = "contact"),path('about',views.about,name = "about"),] +  static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...