我正在尝试通过表单编辑现有对象。除了没有用当前值填充 ImageField 之外,一切都运行良好。
这是模型:
class Post(models.Model):
author = models.ForeignKey(User, editable=False)
slug = models.SlugField(max_length = 110, editable=False)
title = models.CharField(verbose_name='Blog Title', max_length=40, blank=False)
body = models.TextField(verbose_name='Body')
thumbnail = models.ImageField(upload_to = 'posts/%Y/%m')
这是视图
@login_required
def edit_profile(request, form_class=UserProfileForm, success_url=None, template_name='profiles/edit_profile.html', extra_context=None):
try:
profile_obj = request.user.get_profile()
except ObjectDoesNotExist:
return HttpResponseRedirect(reverse('profiles_create_profile'))
if success_url is None:
success_url = reverse('profiles_profile_detail',
kwargs={ 'username': request.user.username })
if form_class is None:
form_class = utils.get_profile_form()
if request.method == 'POST':
form = form_class(data=request.POST, files=request.FILES, instance=profile_obj)
if form.is_valid():
form.save()
return HttpResponseRedirect(success_url)
else:
form = form_class(instance=profile_obj)
if extra_context is None:
extra_context = {}
context = RequestContext(request)
for key, value in extra_context.items():
context[key] = callable(value) and value() or value
return render_to_response(template_name,
{ 'form': form,
'profile': profile_obj, },
context_instance=context)
该对象确实附加了缩略图,但是当我进行编辑时,缩略图字段中没有显示任何内容