Code

Opened 6 years ago

Closed 3 years ago

#7511 closed New feature (wontfix)

Automatically generate templates for newforms

Reported by: Daniel Pope <dan@…> Owned by: nobody
Component: Forms Version: master
Severity: Normal Keywords: wishlist
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

One of the problems with using newforms at the moment is that while the form.as_*() methods offer a shortcut for generating a functional form, there is no simple migration path to go from using {{form.as_p}} in templates, to the template syntax for the same, as is necessary for further customisation.

I suggest there should be a management command to generate template code for a given form:

With this in myapp/myproject/forms.py:

class MyForm(forms.Form):
    name = forms.CharField(help_text=u"Lorem ipsum dolor sit amet")
    type = forms.ChoiceField(choices=[('a', 'Type A'), ('b', 'Type B')], widget=forms.RadioSelect)

you might do:

$ python manage.py formtemplate myapp.myproject.forms.MyForm
<p>{{form.name.label_tag}} {{form.name}}</p>
<p class="help_text">{{form.name.help_text}}</p>
{{form.name.errors}}

<p>{{form.type.label_tag}}</p>
{{form.type}}
{{form.type.errors}}

This could be pasted or piped into a template.

There is some complication for the same reason as #6388, as illustrated above, and the choice of what to output statically at compile time and what to determine at runtime is somewhat arbitrary. But the aim of this tool would be to assist in generating something editable, so if the form has been made dynamic in some way (eg. self.fields modified in the form constructor) this would still provide a useful foundation to customise manually.

Attachments (0)

Change History (4)

comment:1 Changed 6 years ago by adamfast

  • milestone set to post-1.0
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

comment:2 Changed 5 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:3 Changed 3 years ago by lukeplant

  • Severity set to Normal
  • Type set to New feature

comment:4 Changed 3 years ago by carljm

  • Easy pickings unset
  • Resolution set to wontfix
  • Status changed from new to closed
  • UI/UX unset

This is a great example of the kind of utility that would be a really nice contribution as an external app for those who want it, but has no need to be in core.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.