我正在使用Django 1.11并创建了一个包含formset的模板。一切正常。 但我现在已经将Django升级到Django 2.0,我的模板提出了:
序列项5:预期的str实例,找到BoundField
我不知道为什么在使用Django 1.11时会出现这样的错误
这是我的模板:
<table style ="border-collapse: separate;border-spacing: 15px;" id="id_forms_table">
<tr><td width="5%">N P</td><td width="8%">Date d'operation</td><td width="25%">Désignation</td><td width="10%">Type tiers</td><td width="10%">Tiers</td><td width="10%">Référence de Facture</td><td width="10%">Montant Débit</td><td width="10%">Montant Crédit</td></tr>
{% for form in formset %}
<tr style="border:1px solid black;" id="{{ form.prefix }}-row" class="dynamic-form" >
<td{% if forloop.first %} class="" {% endif %}><div class="col-xs-1"><b><p name="np1">1</p></b></div></td>
<td>
{% render_field form.dateOperation1 class="form-control" id="inlineFormInputName" name="designation1" %}{{form.dateOperation1.errors}}
</td>
<td>{% render_field form.designation1 class="form-control" id="inlineFormInputName" placeholder="Designation opération" name="datefacture1" %}{{form.designation1.errors}}
</td>
<td>
{% render_field form.typeTiers1 class="form-control" id="inlineFormInputName" placeholder="Montant HT" %}{{form.typeTiers1.errors}}
</td>
<td>
{% render_field form.tiers1 class="form-control" id="inlineFormInputName" placeholder="TVA" name="tiers" %}{{form.tiers1.errors}}
</td>
<td>{% render_field form.numfacture1 class="form-control" id="inlineFormInputName" placeholder="" name="mttc1" %}{{form.numfacture1.errors}}
</td>
<td>{% render_field form.montantdebit1 class="form-control" id="inlineFormInputName" placeholder="Montant" name="mttc1" %}{{form.montantdebit1.errors}}
</td>
<td>{% render_field form.montantcredit1 class="form-control" id="inlineFormInputName" placeholder="Montant" name="mttc1" %}{{form.montantcredit1.errors}}
</td>
</tr>
{% endfor %}<tr>
</tr></table>
forms.py:
class FormBanque(forms.Form):
dateOperation1=forms.DateField(initial=datetime.date.today)
designation1=forms.CharField()
typeTiers1=MyModelChoiceField1(queryset=operation_Bancaire.objects.all(),to_field_name='type_tiers',required=False)
tiers1=forms.ModelChoiceField(queryset=operation_Bancaire.objects.none())
numfacture1=forms.ModelChoiceField(queryset=client.objects.all().values_list('nom', flat=True),required=False)
montantdebit1=forms.CharField()
montantcredit1=forms.CharField()
BanqueFormSet=formset_factory(FormBanque)
以下是我的观点:
class BanqueViews(TemplateView):
def get(self, request,*args,**kwargs):
if request.user.is_active :
current_user = request.user
form1=banque()
form2=AjoutFrs()
formset = BanqueFormSet()
context={"formset":formset,"current_user":current_user,"form2":form2,"form1":form1}
return render(request,"appOne/banquemineFormsPy.html", context)
这里是完整的追溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/banque
Django Version: 2.0.1
Python Version: 3.6.2
Installed Applications:
['appOne',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'widget_tweaks']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template C:\version21\DjangoProj\tsite\appOne\templates\appOne\banquemineFormsPy.html, error at line 71
sequence item 5: expected str instance, BoundField found
61 :
62 :
63 :
64 :
65 : <br>
66 :
67 : <!-- Formulaire d'ajout des liiigneee <td width="10%"
colspan="2">Importer la piece Justificatif</td> -->
68 : <table style ="border-collapse: separate;border-spacing: 15px;"
id="id_forms_table">
69 :
70 : <tr><td width="5%">N P</td><td width="8%">Date
d'operation</td><td width="25%">Désignation</td><td width="10%">Type tiers</td><td width="10%">Tiers</td><td width="10%">Référence de Facture</td><td width="10%">Montant Débit</td><td width="10%">Montant Crédit</td></tr>
71 : {% for form in formset %}
72 : <tr style="border:1px solid black;" id="{{ form.prefix }}-
row" class="dynamic-form" >
73 :
74 : <td{% if forloop.first %} class="" {% endif %}><div
class="col-xs-1"><b><p name="np1">1</p></b></div></td>
75 :
76 : <td>
77 : {% render_field form.dateOperation1 class="form-
control" id="inlineFormInputName" name="designation1" %}
{{form.dateOperation1.errors}}
78 :
79 : </td>
80 : <td>{% render_field form.designation1 class="form-
control" id="inlineFormInputName" placeholder="Designation opération"
name="datefacture1" %}{{form.designation1.errors}}
81 :
Traceback:
File "C:\Users\users MGE\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\users MGE\AppData\Local\Programs\Python\Python36-
32\lib\site-
packages\django\core\handlers\base.py" in _get_response
128. response =
self.process_exception_by_middleware(e, request)
File "C:\Users\users MGE\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request,
*callback_args, **callback_kwargs)
File "C:\Users\users MGE\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\views\generic\base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "C:\Users\users MGE\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "C:\version21\DjangoProj\tsite\appOne\views.py" in get
178. return render(request,"appOne/banquemineFormsPy.html", context)
File "C:\Users\users MGE\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\shortcuts.py" in render
36. content = loader.render_to_string(template_name, context,
request, using=using)
File "C:\Users\users MGE\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\template\loader.py" in render_to_string
62. return template.render(context, request)
File "C:\Users\users MGE\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\template\backends\django.py" in render
61. return self.template.render(context)
File "C:\Users\users MGE\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\template\base.py" in render
175. return self._render(context)
File "C:\Users\users MGE\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\template\base.py" in _render
167. return self.nodelist.render(context)
File "C:\Users\users MGE\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\template\base.py" in render
943. bit = node.render_annotated(context)
File "C:\Users\users MGE\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\template\base.py" in render_annotated
910. return self.render(context)
File "C:\Users\users MGE\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\template\loader_tags.py" in render
58. result = self.nodelist.render(context)
File "C:\Users\users MGE\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\template\base.py" in render
943. bit = node.render_annotated(context)
File "C:\Users\users MGE\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\template\base.py" in render_annotated
910. return self.render(context)
File "C:\Users\users MGE\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\template\defaulttags.py" in render
218. return mark_safe(''.join(nodelist))
Exception Type: TypeError at /banque
Exception Value: sequence item 5: expected str instance, BoundField
found
你能帮我注意一下这个问题是什么吗?我遇到了一个我没想到的问题
谢谢
答案 0 :(得分:1)
将django 1.11升级到django 2.1之后,我遇到了类似的问题。下面的调整有帮助。
更改了widget_tweaks.py的最后一行
return bounded_field
至
return str(bounded_field)
。
答案 1 :(得分:1)
我曾经在pythonanywhere中遇到过这个问题。
您可以只更新django-widget-tweaks
pip upgrade django-widget-tweaks
答案 2 :(得分:0)
你似乎在django-widget-tweaks中遇到了this issue。它已在pull request 57中修复,但看起来还没有发布。
如果你想继续使用Django 2.0,你可以尝试安装master分支,或者你可以试试pull请求中提到的fork django-widgets-improved
。
另一个选择是暂时降级到1.11 LTS。
答案 3 :(得分:0)
我已经通过回到Django 1.11而不是2.0
解决了这个问题