【问题标题】:Error during template rendering: __str__ returned non-string (type NoneType)模板渲染期间出错:__str__ 返回非字符串(NoneType 类型)
【发布时间】:2019-11-04 03:52:24
【问题描述】:

我有一个问题,我可以在我的数据库为空的情况下渲染所有页面。但是当我引入数据时,我遇到了一个问题:渲染模板时出错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)

 ...

▶ 本地变量 C:\Program Files\Python37\lib\site-packages\django\core\handlers\base.py in _get_response

                response = self.process_exception_by_middleware(e, request)

 ...

▶ 本地变量 C:\Program Files\Python37\lib\site-packages\django\core\handlers\base.py in _get_response

                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)

 ...

▶ 本地变量 C:\Program Files\Python37\lib\site-packages\django\template\base.py in _render

        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)

 ...

▶ 本地变量 C:\Program Files\Python37\lib\site-packages\django\template\base.py in _render

        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,

 ...

▶ 本地变量 C:\Program Files\Python37\lib\site-packages\django\forms\forms.py in _html_output

                    'field_name': bf.html_name,

 ...

▶ 本地变量 C:\Program Files\Python37\lib\site-packages\django\utils\html.py in

    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)

 ...

▶ 本地变量 C:\Program Files\Python37\lib\site-packages\django\forms\widgets.py 在 optgroups 中

        for index, (option_value, option_label) in enumerate(self.choices):

 ...

▶ 本地变量 iter

中的 C:\Program Files\Python37\lib\site-packages\django\forms\models.py
            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)

 ...

▶ 局部变量

【问题讨论】:

    标签: django


    【解决方案1】:

    您的所有__str__ 方法都需要像这样的return 语句

    def __str__(self):
        return self.libelle_filiere
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 2021-11-09
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多