这是我之前问的一个重复问题。但是,我相信我对代码进行了足够的更改,以保证在收到新的错误消息时提出一个新问题。
这是我在views.py(teachers.py)中的代码:
# edit mentor profile
def edit_user(request):
user = request.user
# form = MentorProfileForm(instance=user)
if request.method == 'POST':
form = UserForm(request.POST, instance=user)
mentorform = MentorProfileForm(request.POST, request.FILES, instance=user)
if form.is_valid() and mentorform.is_valid():
form.save()
mentorform.save()
messages.success(request, ('Your profile was successfully updated!'))
return HttpResponseRedirect('%s' % (reverse('profile')))
else:
messages.error(request, ('Please correct the error below.'))
else:
form = UserForm(request.POST, instance=user)
mentorform = MentorProfileForm(request.POST, request.FILES, instance=user)
return render(request, 'classroom/teachers/app-instructor-profile.html', {'form': form,
'mentor_form': mentorform})
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)
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/>
...
这是图像应出现的位置
<img src="{{ mentorform.photo}}" alt="people" class="img-circle width-80" />
urls.py:
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)
这是我在settings.py中添加媒体网址的地方
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')
请注意,我已经编辑了Httpredirect以重定向到编辑用户,但仍然无法更新个人资料图片
return HttpResponseRedirect('%s' % (reverse('teachers:edit_user')))
更新
我尝试对urls.py进行一些更改-如果我不检查调试,也许无法提供我的媒体文件,但是这并没有改变任何内容,仍然没有更新用户的个人资料图片。
我还注意到,当我检查图像应该出现在网页上的位置时,我得到了
<img src=(unknown) alt="people" class="img-circle width-80">
我注意到我还没有创建媒体文件夹,所以我手动创建了该文件夹,但是仍然没有更新我的个人资料图片。也许问题出在我用html调用图片?
答案 0 :(得分:0)
将此添加到您的模型中。py:
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
答案 1 :(得分:0)
您在url设置中有一个教师的名称空间,因此在调用url时也要添加名称空间。
return HttpResponseRedirect('%s' % (reverse('teachers:profile')))