我有一个表单集,可以将其渲染为模板,并且在提交模板时会在数据库中对其进行更新。但是,页面只是刷新,我似乎无法将其转到另一页。我将表单集保存在formset.is_valid()
内部的get_context_data
部分中。我创建了一个def get_success_url(self):
,但是即使成功也没有发生。我在HTML页面中使用{% for erro in form.errors %}
,告诉我没有收到错误。加上它正在保存到数据库!但是问题仍然存在:
点击“提交”后如何将其还原到新页面?
html
<form method="post">
{% csrf_token %}
...
<button type="submit" class="btn btn-primary">Submit</button>
</form>
views.py
class LibraryUpdateView(LoginRequiredMixin, UpdateView):
model = Book
form_class = BookForm
template_name = 'library/library_update.html'
def get_context_data(self, *args, **kwargs):
context = super(LibraryUpdateView, self).get_context_data(*args, **kwargs)
print(self.object)
print('hi')
qs = Library.objects.filter(
library_id = self.kwargs.get('library_id')
)
if self.request.POST:
context['getperinfo_form'] = BookFormSet(self.request.POST,instance=qs.first())
formset = BookFormSet(self.request.POST,instance=qs.first())
############ THIS DOES HAPPEN ################
if formset.is_valid():
print('we valid?')
formset.save()
else:
print('we not valid')
messages.api.add_message(self.request, messages.ERROR, "Something is wrong!!!")
else:
qs = Library.objects.filter(
library_id = self.kwargs.get('library_id')
)
context['getperinfo_form'] = BookInfoFormSet(instance=qs.first())
context['hi']=3
return context
def form_valid(self, form):
##### THIS SECTION NEVER HAPPENS #####
print('checking form valid')
context = self.get_context_data()
getperinfo_form = context['getperinfo_form']
with transaction.atomic():
self.object = form.save()
if getperinfo_form.is_valid():
getperinfo_form.instance = self.object
getperinfo_form.save()
return super(LibraryUpdateView, self).form_valid(form)
def get_object(self):
#qs = Contracts.objects.filter(pk=self.request.user).first()
qs = Library.objects.filter(
library_id = self.kwargs.get('library_id')
)
if qs.count() == 1:
if self.request.user == qs.first().contract_id.skilledlaborer.user or self.request.user == qs.first().contract_id.inhouselandmanager.user:
pass
qs2 = BookInfo.objects.filter(
library_id__library_id = self.kwargs.get('library_id')
)
else:
raise PermissionDenied
return qs2.first()
raise Http404
def get_success_url(self):
####### THIS NEVER HAPPENS ############
print('about to reverse')
return reverse("library:create-list")