问题描述
我的表单集基于CRUD方法.从这里到那里获取代码.我无法理解它所产生的错误:
I have based my formset on the CRUD methodology. Taken code from here and there. I can't understand the Error that it is producing:
Internal Server Error: /create/(?P2[\w-]+)/$
Traceback (most recent call last):
File "C:\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Python39\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python39\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Python39\lib\site-packages\django\views\generic\base.py", line 98, in dispatch
return handler(request, *args, **kwargs)
File "C:\Python39\lib\site-packages\django\views\generic\edit.py", line 172, in post
return super().post(request, *args, **kwargs)
File "C:\Python39\lib\site-packages\django\views\generic\edit.py", line 142, in post
return self.form_valid(form)
File "C:\Projects\hosp_app\doc_aide\views.py", line 84, in form_valid
return super().form_valid(form)
File "C:\Python39\lib\site-packages\django\views\generic\edit.py", line 126, in form_valid
return super().form_valid(form)
File "C:\Python39\lib\site-packages\django\views\generic\edit.py", line 57, in form_valid
return HttpResponseRedirect(self.get_success_url())
File "C:\Python39\lib\site-packages\django\http\response.py", line 465, in __init__
self['Location'] = iri_to_uri(redirect_to)
File "C:\Python39\lib\site-packages\django\utils\encoding.py", line 147, in iri_to_uri
return quote(iri, safe="/#%[]=:;$&()+,!?*@'~")
File "C:\Python39\lib\urllib\parse.py", line 853, in quote
return quote_from_bytes(string, safe)
File "C:\Python39\lib\urllib\parse.py", line 878, in quote_from_bytes
raise TypeError("quote_from_bytes() expected bytes")
TypeError: quote_from_bytes() expected bytes
它基于的Formset类如下:
The Formset class on which it is based is as follows:
表单和视图如下:
class PrescriptionCreate(generic.CreateView):
model = Prescription
template_name = 'doc_aide/write_prescription4.html'
form_class = PrescriptionForm
def get_context_data(self, **kwargs):
print('here')
context = super().get_context_data(**kwargs)
if self.request.POST:
context['line_prescription'] = SinglePrescriptionFormset(self.request.POST)
else:
context['line_prescription'] = SinglePrescriptionFormset()
context['form'].fields['patient'].initial = Patient.objects.get(pk=self.kwargs['patient'])
return context
def form_valid(self, form):
print('Ia am here')
context = self.get_context_data()
prescriptionlines = context['line_prescription']
with transaction.atomic():
form.instance.created_by = self.request.user
self.object = form.save()
if prescriptionlines.is_valid():
prescriptionlines.instance = self.object
prescriptionlines.save()
return super().form_valid(form)
def get_success_url(self):
return reverse_lazy('doc_aide:prescription_detail', kwargs={'pk': self.object.pk})
有人可以帮忙吗?
推荐答案
在您的 get_success_url
方法中,您正在使用 reverse_lazy
返回url,该URL返回对象 get_success_url
应该返回字符串.您应该改用 reverse
:
In your get_success_url
method you are returning a url using reverse_lazy
, which returns an object, get_success_url
is supposed to return a string. You should use reverse
instead:
def get_success_url(self):
return reverse('doc_aide:prescription_detail', kwargs={'pk': self.object.pk})
最后,在表单有效方法中,您还将使用 return super().form_valid(form)
,这可能会给您带来麻烦,因为超类的form_valid方法也将保存表格.而是将其更改为 return HttpResponseRedirect(self.get_success_url())
.
Also in your form valid method at the end you are using return super().form_valid(form)
which might cause some trouble for you, as the form_valid method of the super class will also save the form. Instead change it to return HttpResponseRedirect(self.get_success_url())
.
这篇关于form_valid导致TypeError:quote_from_bytes()预期字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!