Python 不更新Django中的配置文件图片

Python 不更新Django中的配置文件图片,python,django,django-models,django-forms,django-views,Python,Django,Django Models,Django Forms,Django Views,这是对我之前提出的一个问题的重复。然而,我相信我对代码做了足够大的修改,当我收到新的错误消息时,我有理由问一个新问题 这是我在views.py(teachers.py)中的代码: forms.py #basic form class UserForm(forms.ModelForm): class Meta: model = User fields = ('first_name','last_name','email') # edit mentor pr

这是对我之前提出的一个问题的重复。然而,我相信我对代码做了足够大的修改,当我收到新的错误消息时,我有理由问一个新问题

这是我在views.py(teachers.py)中的代码:

forms.py

#basic form
class UserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('first_name','last_name','email')

# edit mentor profile
class MentorProfileForm(forms.ModelForm):
    class Meta:
        model = Mentor
        fields = ('photo',)
models.py:

class User(AbstractUser):
    is_student = models.BooleanField(default=False)
    is_teacher = models.BooleanField(default=False)
...
class Mentor(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True)
    linkedin = models.URLField(max_length=200,null=True,blank=True)
    photo = models.ImageField(null=True,blank=True,upload_to='media')

    def __str__(self):
        return "Profile of user {}".format(self.user.username)

@receiver(post_save,sender=User)
def create_or_update(sender, instance,created, **kwargs):
    if created:
        post_save.connect(create_or_update, sender=User)
<img src=(unknown) alt="people" class="img-circle width-80">
html表单:

<form id="edit-mentor-profile" class="form-horizontal" method="post" enctype="multipart/form-data">
                        {% csrf_token %}
                      <div class="form-group">
                        <label for="photo" class="col-sm-2 control-label">Avatar</label>
                        <div class="col-md-6">
                          <div class="media v-middle">
                            <div class="media-left">
                              <div class="icon-block width-100 bg-grey-100">
                                  <img id="image" style="width:99%;height:99%;">
                              </div>
                            </div>
                            <div class="media-body">
                                <input type="file" id="files" class="btn btn-white btn-sm paper-shadow relative" data-z="0.5" data-hover-z="1" data-animated/>
...
这里是我在settings.py中添加媒体url的地方

path('teachers/', include(([
    path('', teachers.QuizListView.as_view(), name='app-instructor-dashboard'),
    path('logout', teachers.logout_request, name="logout"),
    path('edit_user', teachers.edit_user, name='edit_user'),
], 'classroom'), namespace='teachers'))
...
#user profile image
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
注意:我已经编辑了Httpredirect以重定向到编辑用户,但仍然无法更新配置文件图片

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates')
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
...
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
更新

我试图对URL.py进行一些更改-如果我不检查调试,可能我的媒体文件不会被提供,但是这没有改变任何内容,仍然没有更新用户的配置文件图片

我还注意到,当我运行inspect时,图像应该出现在网页上的什么位置,我得到了这个消息

return HttpResponseRedirect('%s' % (reverse('teachers:edit_user')))


我注意到我没有创建媒体文件夹,所以我手动创建了文件夹,但仍然没有更新我的个人资料图片。也许问题在于我用html调用图片?

将此添加到您的模型中。py:

class User(AbstractUser):
    is_student = models.BooleanField(default=False)
    is_teacher = models.BooleanField(default=False)
...
class Mentor(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True)
    linkedin = models.URLField(max_length=200,null=True,blank=True)
    photo = models.ImageField(null=True,blank=True,upload_to='media')

    def __str__(self):
        return "Profile of user {}".format(self.user.username)

@receiver(post_save,sender=User)
def create_or_update(sender, instance,created, **kwargs):
    if created:
        post_save.connect(create_or_update, sender=User)
<img src=(unknown) alt="people" class="img-circle width-80">
def mentor_照片(实例,文件名):
返回“mentor/photos/%s”%filename
班级导师(models.Model):
...

models.ImageField(null=True,blank=True,upload_to=mentor_photos)#您的url设置中有教师的名称空间,因此在调用url时也添加名称空间

def mentor_photos(instance, filename):
    return 'mentor/photos/%s' % filename

class Mentor(models.Model):
    ...
    models.ImageField(null=True, blank=True, upload_to=mentor_photos)  # <-- Note the upload_to change

你能提供你的url.py代码吗?@LinhNguyen我已经添加了url.py。我在你的url.py中没有看到任何带有
name='profile'
的url。为了使用reverse(),您需要在it@LinhNguyen即使我使用say edit_user,我也会收到错误消息no reverse match。除此之外,如果我完全删除该行,则除了配置文件图像之外,其他所有内容都会更新。当您使用name时,您不使用url,而是使用
name
中的值,因此它是
reverse(“编辑用户”)
不需要相同的错误“未找到“编辑用户”的reverse”“编辑用户”不是有效的视图函数或模式名称。“”。除此之外,即使我完全删除该行,图像也不会更新,但其他所有内容都会更新。问题在于更新配置文件图片,但不是上载到表单的文件不是吗form@Emm再次更改了答案。@Emm问题:你为什么要用yeah查找图像,注意到前一段时间,虽然更改了,但会更新。仍然收到相同的问题,请检查问题,添加了更新-已尝试此。它可以工作,但问题是我的个人资料图片在保存时没有更新。您是否在“设置和url模式”中添加了媒体url?使用我在“设置和url.py”中的内容更新了问题
return HttpResponseRedirect('%s' % (reverse('teachers:profile')))