Why is not there a reusable template for Django's DetailView?

后端 未结 2 1065
生来不讨喜
生来不讨喜 2021-01-18 02:48

Displaying forms in a template is rather easy in Django:

{% csrf_token %} {{ form }}
2条回答
  •  猫巷女王i
    2021-01-18 03:29

    I think it is not as reusable as you imagine.

    It might conceivably be possible to define "standard" ways to render simple model properties like CharField - this quickly becomes impossible when you get into more complex relational fields like ManyToManyField, ForeignKey, OneToOneField. You would end up overriding any default representation very quickly for anything but the simplest of models.

    Secondly Django is not - and should not be - opinionated about what your models are for, and therefore it makes sense that it doesn't try to assume how you want to render them.

    This is different from forms where the structure of individual form fields is defined in Django and in HTML, and there is a strong correlation between the two.

提交回复
热议问题