我有一个Formview
和一个UpdateView
彼此冲突。他们应该一起工作,但不能以这种方式。每当我尝试使用UpdateView
时,值就会通过FormView
进行保存,这会导致其发送回表单验证错误
例如“具有该标题的视频已经存在”。
Forms.py :
class BaseVideoUploadForm(forms.ModelForm):
"""
Form for uploading videos using related to:
:model: 'videos.Video'.
"""
title = forms.CharField(
widget=forms.TextInput(
attrs={'placeholder': 'Blackjack shuffle procedures'}))
description = forms.CharField(
widget=forms.Textarea(attrs={'rows': 3}),
help_text='A description of the content in this video.')
category = forms.ModelChoiceField(
queryset=Category.objects.all(),
required=True)
receiver = forms.ModelMultipleChoiceField(
queryset=EmployeeType.objects.all(),
required=True,
widget=forms.CheckboxSelectMultiple,
help_text='Employees in these groups will be able to view the video.')
video_file = forms.FileField()
thumbnail = forms.ImageField(required=False)
class Meta:
model = Video
fields = (
'title',
'description',
'receiver',
'category',
'video_file',
'thumbnail',
)
widgets = {
'date_time': forms.HiddenInput(),
'sender': forms.HiddenInput(),
'unviewed': forms.HiddenInput(),
'viewed': forms.HiddenInput(),
}
def __init__(self, *args, **kwargs):
super(BaseVideoUploadForm, self).__init__(*args, **kwargs)
self.fields['receiver'].label = "Viewers"
self.fields['receiver'].queryset = EmployeeType.objects.all()
self.fields['category'].queryset = Category.objects.all()
class VideoUploadForm(BaseVideoUploadForm):
send_notifications = forms.BooleanField(
required=False,
help_text='Send viewers a notification about the new video.')
class Meta(BaseVideoUploadForm.Meta):
fields = BaseVideoUploadForm.Meta.fields + ('send_notifications', )
FormView:
class VideoUploadView(FormView):
form_class = VideoUploadForm
success_url = '/videos'
template_name = 'videos/video_form.html'
def get(self, request, *args, **kwargs):
form = self.form_class()
return render(request, self.template_name, {'form': form})
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST, request.FILES)
form.instance.sender = self.request.user
if form.is_valid():
form.save()
# doing stuff
form.save()
messages.success(self.request, self.success_message)
if request.is_ajax():
return JsonResponse({'success': True, 'url': reverse('videos-home')})
else:
return redirect(self.success_url)
else:
if request.is_ajax():
return JsonResponse({'success': False, 'error': form.errors})
else:
return render(request, self.template_name, {'form': form})
UpdateView :
class VideoUpdateView(UpdateView):
model = Video
form_class = VideoUploadForm
Urls.py:
urlpatterns = [
path('', views.VideoListView.as_view(), name='videos-home'),
path('upload/', views.VideoUploadView.as_view(), name='videos-upload'),
path('<int:pk>', VideoDetailView.as_view(), name='videos-detail'),
path('<int:pk>/delete/', VideoDeleteView.as_view(), name='videos-delete'),
path('<int:pk>/viewed/', views.mark_as_viewed, name='mark-as-viewed'),
path('<int:pk>/update/', VideoUpdateView.as_view(), name='videos-update'),
path('<int:pk>/notify', VideoNotificationView.as_view(), name='videos-notify'),
]
如何使UpdateView
不依赖FormView
?还是要使用FormView
? UpdateView
似乎正在指向FormView
。