Herramientas de usuario

Herramientas del sitio


wiki2:python:django:forms

¡Esta es una revisión vieja del documento!


Django Forms

Render a form

  <form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Sign up</button>
  </form>
<form method="post" novalidate>
  {% csrf_token %}
  {{ form }}
  <button type="submit">Submit</button>
</form>

Fíjate aquí, con novalidate no haría la validación de datos local.

   <form method="post" action="{% url 'login' %}">
    {% csrf_token %}

    <div>
      <td>{{ form.username.label_tag }}</td>
      <td>{{ form.username }}</td>
    </div>
    <div>
      <td>{{ form.password.label_tag }}</td>
      <td>{{ form.password }}</td>
    </div>
    <div>
      <input type="submit" value="login" />
      <input type="hidden" name="next" value="{{ next }}" />
    </div>
    </form>
            <form action="{% url 'login' %}" method="POST">
                {% csrf_token %}
                <input type="text" name="username" placeholder="Username">
                <input type="password" name="password" placeholder="Password">
                <input type='submit' value='login'>
            </form>
<form method="post" novalidate>
  {% csrf_token %}

  {{ form.non_field_errors }}

  {% for hidden_field in form.hidden_fields %}
    {{ hidden_field.errors }}
    {{ hidden_field }}
  {% endfor %}

  <table border="1">
    {% for field in form.visible_fields %}
      <tr>
        <th>{{ field.label_tag }}</th>
        <td>
          {{ field.errors }}
          {{ field }}
          {{ field.help_text }}
        </td>
      </tr>
    {% endfor %}
  </table>

  <button type="submit">Submit</button>
</form>

How does it work?

When we write {{ form }} in a template, it’s actually accessing the __str__ method from the BaseForm class. We can use other three methods to render it (as table, as p, as ul tags): as_table(), as_ul(), as_p().

The as_table() and as_ul() methods do not create the <table> and the <ul> tags, so we have to add it by ourselves.

<form method="post" novalidate>
  {% csrf_token %}
  <table border="1">
    {{ form }}
  </table>
  <button type="submit">Submit</button>
</form>

The form definition

class ColorfulContactForm(forms.Form):
    name = forms.CharField(
        max_length=30,
        widget=forms.TextInput(
            attrs={
                'style': 'border-color: blue;',
                'placeholder': 'Write your name here'
            }
        )
    )
    email = forms.EmailField(
        max_length=254,
        widget=forms.EmailInput(attrs={'style': 'border-color: green;'})
    )
    message = forms.CharField(
        max_length=2000,
        widget=forms.Textarea(attrs={'style': 'border-color: orange;'}),
        help_text='Write here your message!'
    )

The validation process...

Converts all the data to proper values in cleaned_data if they are, for sure, valid. An example of a Boolean field:

class MyForm(forms.Form):
    has_code = forms.BooleanField(widget= forms.CheckboxInput())
def my_form_view(request):
    if request.method == 'POST':
        my_form = MyForm(request.POST)
        if my_form.is_valid():
            # my_form.cleaned_data['has_code'] is now a bool
wiki2/python/django/forms.1549711199.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)