我有一个问题,可以在数据库为空的情况下呈现所有页面。但是,当我引入数据时,我遇到了一个问题:渲染模板 str 时出错,返回了非字符串(类型NoneType)。低于在渲染行生成此类错误的代码之一。 预先谢谢你。
models.py
class Filiere(models.Model):
departement=models.ForeignKey( "Departement", verbose_name="Département", on_delete=models.CASCADE)
code_filiere=models.CharField("Code de la filière", max_length=10, unique=True)
libelle_filiere=models.CharField("Libellé de la filière", max_length=100)
def __str__(self):
self.libelle_filiere
#Description d'une option
class Option(models.Model):
filiere=models.ForeignKey("Filiere", on_delete=models.CASCADE, verbose_name='Filière')
niveau=models.ManyToManyField("niveau", through='Posseder_Niveau')
code_option=models.CharField("Code de l'option", max_length=6,unique=True,)
libelle_option= models.CharField("Libelle de l'option", max_length=100)
effectif=models.IntegerField("Effectif", default=0, validators=[
MinValueValidator(limit_value=0 , message=" Attention votre option a un effectif négatif"),
])
def __str__(self):
return self.libelle_option
class Posseder_Niveau(models.Model):
niveau=models.ForeignKey("Niveau", on_delete=models.CASCADE)
option=models.ForeignKey("Option", on_delete=models.CASCADE)
class Niveau(models.Model):
libelle_niveau=models.CharField("Libellé du niveau", max_length=25, unique=True)
semestre=models.ManyToManyField("Semestre", through="Posseder_Semestre")
cursus=models.ForeignKey('Cursus', on_delete=models.CASCADE)
def __str__(self):
self.libelle_niveau
forms.py
class Option_Form(forms.ModelForm):
class Meta:
model=Option
# exclude=("niveau",)
fields='__all__'
def __init__(self, *args,**kwargs):
super().__init__(*args, **kwargs)
for _, value in self.fields.items():
value.widget.attrs['placeholder']=value.label
value.widget.attrs['class'] = 'form-control required'
views.py
def option(request):
# import ipdb; ipdb.set_trace()
f=Option_Form()
if request.method=="POST":
f=Option_Form(request.POST)
if f.is_valid():
f.save()
else:
return render(request, 'configuration/ajout_option.html', {'f': f})
回溯
内部的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ core \ handlers \ exception.py
response = get_response(request)
...
▶本地变量 _get_response中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ core \ handlers \ base.py
response = self.process_exception_by_middleware(e, request)
...
▶本地变量 _get_response中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ core \ handlers \ base.py
response = wrapped_callback(request, *callback_args, **callback_kwargs)
...
▶本地变量 E:\ PLACORESA \ configuration \ views.py选项
return render(request, 'configuration/ajout_option.html', {'f': f})
...
▶本地变量 渲染中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ shortcuts.py
content = loader.render_to_string(template_name, context, request, using=using)
...
▶本地变量 C:\ Program Files \ Python37 \ lib \ site-packages \ django \ template \ loader.py in render_to_string
return template.render(context, request)
...
▶本地变量 渲染中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ template \ backends \ django.py
return self.template.render(context)
...
▶本地变量 渲染中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ template \ base.py
return self._render(context)
...
▶本地变量 _render中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ template \ base.py
return self.nodelist.render(context)
...
▶本地变量 渲染中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ template \ base.py
bit = node.render_annotated(context)
...
▶本地变量 C:\ Program Files \ Python37 \ lib \ site-packages \ django \ template \ base.py in render_annotated
return self.render(context)
...
▶本地变量 渲染中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ template \ loader_tags.py
return compiled_parent._render(context)
...
▶本地变量 _render中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ template \ base.py
return self.nodelist.render(context)
...
▶本地变量 渲染中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ template \ base.py
bit = node.render_annotated(context)
...
▶本地变量 C:\ Program Files \ Python37 \ lib \ site-packages \ django \ template \ base.py in render_annotated
return self.render(context)
...
▶本地变量 渲染中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ template \ loader_tags.py
result = block.nodelist.render(context)
...
▶本地变量 渲染中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ template \ base.py
bit = node.render_annotated(context)
...
▶本地变量 C:\ Program Files \ Python37 \ lib \ site-packages \ django \ template \ base.py in render_annotated
return self.render(context)
...
▶本地变量 渲染中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ template \ base.py
output = self.filter_expression.resolve(context)
...
▶本地变量 C:\ Program Files \ Python37 \ lib \ site-packages \ django \ template \ base.py解析
obj = self.var.resolve(context)
...
▶本地变量 C:\ Program Files \ Python37 \ lib \ site-packages \ django \ template \ base.py解析
value = self._resolve_lookup(context)
...
▶本地变量 _resolve_lookup中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ template \ base.py
current = current()
...
▶本地变量 C:\ Program Files \ Python37 \ lib \ site-packages \ django \ forms \ forms.py in as_p
errors_on_separate_row=True,
...
▶本地变量 _html_output中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ forms \ forms.py
'field_name': bf.html_name,
...
▶本地变量
中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ utils \ html.py klass.__str__ = lambda self: mark_safe(klass_str(self))
...
▶本地变量 str
中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ forms \ boundfield.py return self.as_widget()
...
▶本地变量 C:\ Program Files \ Python37 \ lib \ site-packages \ django \ forms \ boundfield.py in as_widget
renderer=self.form.renderer,
...
▶本地变量 渲染中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ forms \ widgets.py
context = self.get_context(name, value, attrs)
...
▶本地变量 C:\ Program Files \ Python37 \ lib \ site-packages \ django \ forms \ widgets.py in get_context
context = super().get_context(name, value, attrs)
...
▶本地变量 C:\ Program Files \ Python37 \ lib \ site-packages \ django \ forms \ widgets.py in get_context
context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs)
...
▶本地变量 optgroups中的C:\ Program Files \ Python37 \ lib \ site-packages \ django \ forms \ widgets.py
for index, (option_value, option_label) in enumerate(self.choices):
...
▶本地变量 C:\ Program Files \ Python37 \ lib \ site-packages \ django \ forms \ models.py在 it
中 yield self.choice(obj)
...
▶本地变量 C:\ Program Files \ Python37 \ lib \ site-packages \ django \ forms \ models.py选择
return (self.field.prepare_value(obj), self.field.label_from_instance(obj))
...
▶本地变量 C:\ Program Files \ Python37 \ lib \ site-packages \ django \ forms \ models.py in label_from_instance
return str(obj)
...
▶本地变量
答案 0 :(得分:1)
您所有的>>> column
defaultdict(<class 'list'>, {'1': [{'row': 'Product/Descriptian'}], '2': [{'row': 'Qty/unit'}, {'row': 'Text'}], '3': [{'row': ''}]})
>>>
>>> longest_array_len = max((len(v) for v in column.values()))
>>> longest_array_len
2
>>> final = {k: v + [{'row': ''}] * (longest_array_len - len(v)) for k, v in column.items()}
>>>
>>> final
{'1': [{'row': 'Product/Descriptian'}, {'row': ''}], '2': [{'row': 'Qty/unit'}, {'row': 'Text'}], '3': [{'row': ''}, {'row': ''}]}
方法都需要这样的__str__
语句
return