简体   繁体   中英

Custom Templating

I want to customize the default form template rendering that GAE does when using its version of Django. I'm using Django 1.3 in GAE.

I have an "input" page that loads app engine templates and calls a "parameters" module that defines the form inputs. Here is the middle of the input page that renders the form:

html = html + template.render(templatepath + '04input_start.html', {})
html = html + str(model_parameters.ModelInp())

Here is model_parameters.ModelInp():

class ModelInp(forms.Form):
    activeIngredient = forms.CharField(widget=forms.Textarea (attrs={'cols': 20, 'rows': 1}), label='Active Ingredient')
    expDuration = forms.ChoiceField(required=True, label='Exposure Duration', choices=expDuration_CHOICES, initial='Intermediate-Term')

The result is a HTML form where each variable above is table row with the label as 1 column and the CharField/ChoiceField as the other 1 column. Essentially:

<tr>
    <th>
        <label for="id_activeIngredient">Active Ingredient:</label>
    </th>
    <td>
        <textarea rows="1" cols="20" name="activeIngredient" id="id_activeIngredient"></textarea>
    </td>
</tr>
<tr>
    <th>
        <label for="id_expDuration">Exposure Duration:</label>
    </th>
    <td>
        <select name="expDuration" id="id_expDuration">
            <option value="0">Short-Term</option>
            <option value="1">Intermediate-Term</option>
            <option value="2">Long-Term</option>
        </select>
    </td>
</tr>

How would I go about rendering this form in a custom Django template to format the form in a different format other than the simple 2 column default?

GAE doesn't do any "default template rendering". This is all Django.

You are doing something very bizarre in that snippet from the view. The whole point of templates is that you build up the entire HTML page within the template. Why are you concatenating different bits of HTML together in the view? That defeats the purpose of using templates.

Instead of simply concatenating the string representation of the template, you should pass the instantiated form to the template context. Then you can iterate over its fields, or specify them all individually so that you can wrap them in whatever layout you like. This is all well discussed in the Django docs for displaying a form in a template .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM