在Python Django中显示用户上传的图像

在Python Django中显示用户上传的图像,python,django,web,Python,Django,Web,我正在使用Django python框架创建一个网站,目前遇到了一个问题。 我正在使用函数视图显示一个名为myaccount的页面,在“myaccount”上,我希望使用上下文对象显示所有用户详细信息,因为此页面是“user”。我还有另一个名为Profile的模型,它保存了用户的Profile图片和出生日期。但是,当我尝试显示在创建帐户期间上载到名为“/media/%y/%m/%d/imagename.filextension”的媒体文件夹中的图像时,我收到一个错误,提示“profilepict

我正在使用Django python框架创建一个网站,目前遇到了一个问题。 我正在使用函数视图显示一个名为myaccount的页面,在“myaccount”上,我希望使用上下文对象显示所有用户详细信息,因为此页面是“user”。我还有另一个名为Profile的模型,它保存了用户的Profile图片和出生日期。但是,当我尝试显示在创建帐户期间上载到名为“/media/%y/%m/%d/imagename.filextension”的媒体文件夹中的图像时,我收到一个错误,提示“profilepicture”属性没有与其关联的文件。”我一直在搜索这个问题的修复程序,到目前为止还没有找到有效的结果,我尝试创建一个属性函数,通过执行user.profile.profilepicture.get_absolute_url,从名为“get_absolute_url”的图像中获取url,但它失败并显示相同的错误。我想知道是否有人能为我指出解决这个问题或解决方案的方向

我也有枕头安装

下面是显示图像、views.py和url.py的代码

views.py

@login_required
def myaccount(request):
    return render(request, 'account/myaccount.html', {'section': 'myaccount'})
url.py

path('', views.myaccount, name='myaccount'),
myaccount.html

<img src="{{ user.profile.profilepicture.url }}" width="260" height="234"/>
用户的模型来自“django.contrib.auth.models”

为了确认上下文对象是否有问题,我尝试显示用户的名字,该名字按预期工作

帐户注册表视图

def register(request):
    if request.method == 'POST':
        user_form = UserRegistrationForm(request.POST)
        if user_form.is_valid():
            # Create a new user object but avoid saving it yet
            new_user = user_form.save(commit=False)
            # Set the chosen password
            new_user.set_password(
                user_form.cleaned_data['password'])
            # Save the User object
            new_user.save()
            Profile.objects.create(user=new_user)
            # Create the user profile
            return render(request,
                          'account/register_done.html',
                          {'new_user': new_user})
    else:
        user_form = UserRegistrationForm()
    return render(request,
                  'account/register.html',
                  {'user_form': user_form})

如果需要任何其他代码或信息,请询问。

由于您使用的是ImageField,您是否也使用PIP安装Pillow安装了Pillow库?

使用一些常识后,我意识到我实际上没有为profile picture属性上传照片,这将解释为什么没有用于该属性的url。。。无论如何,感谢您的帮助,也很抱歉浪费了时间。如果没有与配置文件相关联的图片,我现在还使用了if块来阻止运行错误。

我已经安装了pillow,但我不确定它的实际用途。pillow是一个用Python处理图像的图像库。一旦安装了Pillow,您就可以使用“from PIL import Image”将其导入到models.py文件中。我建议使用它来处理Python中Django框架的图像文件。有关更多详细信息,请查看相关文档。有没有可能给我一个演示,或者文档涵盖了所有内容?我快速浏览了一下,我认为这并不是我问题的真正解决方案,我并不是说它只是以一种粗鲁的方式收集文件的url,这似乎是我每次遇到的问题,但是它对收集url仍然有用吗?您是否能够显示从浏览器回溯得到的确切错误?
def register(request):
    if request.method == 'POST':
        user_form = UserRegistrationForm(request.POST)
        if user_form.is_valid():
            # Create a new user object but avoid saving it yet
            new_user = user_form.save(commit=False)
            # Set the chosen password
            new_user.set_password(
                user_form.cleaned_data['password'])
            # Save the User object
            new_user.save()
            Profile.objects.create(user=new_user)
            # Create the user profile
            return render(request,
                          'account/register_done.html',
                          {'new_user': new_user})
    else:
        user_form = UserRegistrationForm()
    return render(request,
                  'account/register.html',
                  {'user_form': user_form})