Django表单:在提交db之前要求确认 [英] Django form: ask for confirmation before committing to db
问题描述
更新:该解决方案可以作为单独的答案找到
Update: The solution can be found as a separate answer
我正在制作一个Django表单,以允许用户将tvshow添加到我的数据库。要做到这一点,我有一个 Tvshow
模型,一个 TvshowModelForm
,我使用通用的基于类的视图 CreateTvshowView
/ UpdateTvshowView
生成表单。
I am making a Django form to allow users to add tvshows to my db. To do this I have a Tvshow
model, a TvshowModelForm
and I use the generic class-based views CreateTvshowView
/UpdateTvshowView
to generate the form.
现在来我的问题:让我们说用户想要添加一个节目到数据库,例如权力的游戏。如果此标题的显示已经存在,我想提示用户确认这确实是不同于数据库中的显示,如果没有类似的显示,我想提交到数据库。如何最好地处理这个确认?
Now comes my problem: lets say a user wants to add a show to the db, e.g. Game of Thrones. If a show by this title already exists, I want to prompt the user for confirmation that this is indeed a different show than the one in the db, and if no similar show exists I want to commit it to the db. How do I best handle this confirmation?
我的一些实验显示在下面的代码中,但也许我会以错误的方式进行。我的解决方案的基础是包括一个隐藏的字段 force
,如果用户得到提示,如果他确定他想提交这些数据,应该设置为1,这样我可以读出这个事情是否为1,以决定用户是否点击再次提交,从而告诉我他想要存储它。
Some of my experiments are shown in the code below, but maybe I am going about this the wrong way. The base of my solution is to include a hidden field force
, which should be set to 1 if the user gets prompted if he is sure he wants to commit this data, so that I can read out whether this thing is 1 to decide whether the user clicked submit again, thereby telling me that he wants to store it.
我很想听听你的意见家伙想想如何解决这个问题。
I would love to hear what you guy's think on how to solve this.
views.py
class TvshowModelForm(forms.ModelForm):
force = forms.CharField(required=False, initial=0)
def __init__(self, *args, **kwargs):
super(TvshowModelForm, self).__init__(*args, **kwargs)
class Meta:
model = Tvshow
exclude = ('user')
class UpdateTvshowView(UpdateView):
form_class = TvshowModelForm
model = Tvshow
template_name = "tvshow_form.html"
#Only the user who added it should be allowed to edit
def form_valid(self, form):
self.object = form.save(commit=False)
#Check for duplicates and similar results, raise an error/warning if one is found
dup_list = get_object_duplicates(Tvshow, title = self.object.title)
if dup_list:
messages.add_message(self.request, messages.WARNING,
'A tv show with this name already exists. Are you sure this is not the same one? Click submit again once you\'re sure this is new content'
)
# Experiment 1, I don't know why this doesn't work
# form.fields['force'] = forms.CharField(required=False, initial=1)
# Experiment 2, does not work: cleaned_data is not used to generate the new form
# if form.is_valid():
# form.cleaned_data['force'] = 1
# Experiment 3, does not work: querydict is immutable
# form.data['force'] = u'1'
if self.object.user != self.request.user:
messages.add_message(self.request, messages.ERROR, 'Only the user who added this content is allowed to edit it.')
if not messages.get_messages(self.request):
return super(UpdateTvshowView, self).form_valid(form)
else:
return super(UpdateTvshowView, self).form_invalid(form)
推荐答案
我将其发布为答案。在您的表单的清洁
方法中,您可以按照所需的方式验证用户的数据。它可能是这样的:
I will post it as an answer. In your form's clean
method you can validate user's data in the way you want. It might look like that:
def clean(self):
# check if 'force' checkbox is not set on the form
if not self.cleaned_data.get('force'):
dup_list = get_object_duplicates(Tvshow, title = self.object.title)
if dup_list:
raise forms.ValidationError("A tv show with this name already exists. "
"Are you sure this is not the same one? "
"Click submit again once you're sure this "
"is new content")
这篇关于Django表单:在提交db之前要求确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!