viewflow / django-material

Material Design for Django
http://viewflow.io/
BSD 3-Clause "New" or "Revised" License
2.48k stars 425 forks source link

Use viewset.layout in the detail view #194

Open kmmbvnr opened 7 years ago

kmmbvnr commented 7 years ago

https://gitter.im/viewflow/django-material

Jose Juan Tapia @jjtapia 02:47 Another question. I noticed that the default view contains a simple enumeration of the model fields from a given entry like so: http://forms.viewflow.io/integration/sea/11/detail/ while the change option contained the layout defined in views.py: http://forms.viewflow.io/integration/sea/11/change/ is there a way of making this change layout the default that is shown in .../detail?

marctc commented 7 years ago

I'm working on this but my approach needs that you have to pass form_class to DetailView and then rewrite all the templates in material/fields in order to only print the label and the value. What do you think? Could be useful for you?

cheers!

kmmbvnr commented 7 years ago

Yeah, that's the hard question.

To have reasonable looking good detail view by default is the nice feature, but anyway it's supposed to be overridden in real projects. So it won't be good if the solution for this optional feature would overcomplicate the other part of the codebase.

Currently the layout primitives Row/Column/Fieldset are too bound to the form rendering. From another side, getting a text representation for a model value is hard without a widget. So, there is no simple way to reuse a layout without form instance. But I don't think that the detail view should look exactly like a form but with all widgets disabled. That's is too crudy. Just a disabled text inputs would be enough for most cases.

Your approach looks reasonable, as implementing a template pack that renders a form in a read-only state, without actually do the tricks on a python side.

{% form template='material/readonly/form.html' %}

This would be the really useful feature, in many scenarios beyond the frontend.detail view.

It looks that only a few templates could be overridden. django-material lookup for the template according to a class inheritance mro. So the material/readonly/fields/django_widget.html is the catch-all template.

django-material have some code to get a field value for the list view. You can take a look what django have in django.contrib.admin.utils.display_for_field and django.contrib.admin.utils.display_for_value and what placed in the material.frontend.views.list.DataTableMixin.display_for_field

We can move display_for_field code to the utility module and expose it as a template tag to use it in a widget generic template. If it isn't enough, few templates for a Select/FK could also be added.

marctc commented 7 years ago

Hi,

But I don't think that the detail view should look exactly like a form but with all widgets disabled.

Absolutely agree, thats why I want to replace field template with non-form elements (inputs, radios, etc).

Take a look at the PoC:

https://github.com/APSL/django-material/commit/789fd1b1225cbf2c2f356ab093375e00c0865f70

This is that's look like:

form: selection_040

detail: selection_041

kmmbvnr commented 7 years ago

{% form template='' %} sets the base path for the whole template_pack. So, there is possibility to write {% form template='bootstrap/form.html' %} to render a boostrap styled form with django-material =)

Check the template selection code; it chooses the most specific template for the widget. If the template pack contains a single fields/django_widget.html file, the template would be selected for all form fields.

marctc commented 7 years ago

{% form template='' %} sets the base path for the whole template_pack. So, there is possibility to write {% form template='bootstrap/form.html' %} to render a boostrap styled form with django-material =)

cool!

Check the template selection code; it chooses the most specific template for the widget. If the template pack contains a single fields/django_widget.html file, the template would be selected for all form fields.

Are you sure about that? For an input field, it drops me the following:


template_names
Out[2]: 
['material/frontend/detail/fields/django_textinput.html',
 'material/frontend/detail/fields/django_input.html',
 'material/frontend/detail/fields/django_charfield/django_textinput.html',
 'material/frontend/detail/fields/django_charfield/django_input.html']
kmmbvnr commented 7 years ago

ups, that's b/c we have -2 here

It could be simply adjusted to -1

marctc commented 7 years ago

Yeah, it worked. Thanks

kmmbvnr commented 7 years ago

Several material-design samples of the detail view

material-design-sketch-template material-design-view