我正在尝试使用Django内置类UpdateView
来更新图像模型
模型是:
def get_image_path(instance, filename):
return '/'.join(['studyoffer_images', instance.study_offer.slug, filename])
class UploadStudyOffer(models.Model):
study_offer = models.ForeignKey(StudiesOffert, related_name='uploadsstudyoffer')
image = models.ImageField(upload_to=get_image_path, verbose_name='Seleccionar imagen')
# images folder per object
featured = models.BooleanField(default=False, verbose_name='Destacada',
help_text='Indica si la imagen aparecera en el carrusel')
thumbnail = models.BooleanField(default=False)
active = models.BooleanField(default=True, verbose_name='Activa',
help_text='Indica si una imagen de oferta esta habilitada o disponible')
objects = UploadStudyOfferManager()
def __str__(self):
return self.study_offer.ad_title
此模型的形式为:
class StudyOfferImagesUploadForm(forms.ModelForm):
class Meta:
model = UploadStudyOffer
fields = ('image', 'active', 'featured')
我现在想要的是允许用户编辑图片并进行更改并更改状态图像(featured
,active
,thumbnail
)
然后我构建我的StudyOfferImageUpdateView
来更新图像属性:
class StudyOfferImageUpdateView(SuccessMessageMixin, UserProfileDataMixin, LoginRequiredMixin, UpdateView):
model = UploadStudyOffer
form_class = StudyOfferImagesUploadForm
success_url = reverse_lazy("host:edit-study-offer-image", pk_url_kwarg='pk')
success_message = "Imagen actualizada"
def get_context_data(self, **kwargs):
context = super(StudyOfferImageUpdateView, self).get_context_data(**kwargs)
user = self.request.user
return context
def get_object(self):
return get_object_or_404(UploadStudyOffer, pk=self.kwargs.get('pk'))
访问此视图的URL是:
url(r"^study-offer/edit/images/(?P<pk>\d+)/$",
StudyOfferImageUpdateView.as_view(),
name='edit-study-offer-image'
),
当我想编辑我的图像时,模板是这样的:
{% block body_content %}
<h2>Editar imágen</h2>
<form role="form" action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update" class="submit-button">
</form>
{% endblock %}
此模板已正确重新设置,但是当我按下Update
提交按钮时,出现此错误:
Exception Type: TypeError at /host/study-offer/edit/images/47/
Exception Value: reverse() got an unexpected keyword argument 'pk_url_kwarg'
存在此错误,因为在我的StudyOfferImageUpdateView
中,Django不接受success_url = reverse_lazy("host:edit-study-offer-image", pk_url_kwarg='pk')
我尝试在pk_url_kwarg='pk'
StudyOfferImageUpdateView
句子
q=pk_url_kwarg='pk'
print(q)
我通过浏览器输入StudyOfferImageUpdateView
时的输出是:
# pk string, is not the pk value
pk
System check identified no issues (0 silenced).
October 24, 2017 - 15:58:55
Django version 1.10.7, using settings 'hostayni.settings.development'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[24/Oct/2017 15:58:57] "GET /host/study-offer/edit/images/47/ HTTP/1.1" 200 8559
我知道在get_context_data()
方法中,pk_url_kwarg='pk'
这句话有效,我会在success_url = reverse_lazy("host:edit-study-offer-image", pk_url_kwarg='pk')
中传递它并到达字符串pk
并且这是我的工作流程被打破的原因。
如何在success_url = reverse_lazy("host:edit-study-offer-image", pk_url_kwarg='pk')
中传递预期pk
图像的正确参数?
答案 0 :(得分:2)
首先,当您致电reverse
或reverse_lazy
时,您会在args
和kwargs
中提供参数和关键字参数。你不能pk_url_kwarg='pk'
,你需要做kwargs={'pk_url_kwarg': 'pk'}
。
但是,您不想在'pk_url_kwarg'
字典中使用字符串'pk'
和kwargs
,您希望使用变量self.pk_url_kwarg
和{{1从视图中看。
如果设置self.kwargs['pk']
,则无法访问这些变量,因此您需要覆盖success_url
。
get_success_url
由于您的观点不打算进行子类化和自定义,因此我认为您根本不需要使用def get_success_url(self):
return reverse("host:edit-study-offer-image", kwargs={self.pk_url_kwarg:self.kwargs['pk']})
。我会通过硬编码pk_url_kwarg
来简化代码。
'pk'
或者改为使用def get_success_url(self):
return reverse("host:edit-study-offer-image", kwargs={'pk': self.kwargs['pk']})
:
args