Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Django Forms

Django Forms

Introduction to Django Forms for Django Girls × Taipei.py workshop.

Tzu-ping Chung

February 23, 2016
Tweet

More Decks by Tzu-ping Chung

Other Decks in Programming

Transcript

  1. Django Forms

    View full-size slide

  2. Django Forms
    • Forms?
    • Forms!
    • Model forms

    View full-size slide

  3. Web Page with Form
    submit

    View full-size slide

  4. method="POST"
    enctype="multipart/form-data">



    View full-size slide

  5. method="post"
    enctype="multipart/form-data">
    • Where to send
    • URL
    • Default: "" (current URL)

    View full-size slide

  6. method="post"
    enctype="multipart/form-data">
    • How to send
    • Either get or post
    • Get: postcard
    • Post: Enveloped
    • Default: get

    View full-size slide

  7. method="post"
    enctype="multipart/form-data">
    • How to read
    • application/x-www-form-urlencoded
    • Default
    • multipart/form-data
    • Form contains file

    View full-size slide

  8. GET Submission
    Query string

    View full-size slide

  9. POST Submission
    "GET /admin/ HTTP/1.1" 302 0
    "GET /admin/login/?next=/admin/ HTTP/1.1" 200 1679
    "POST /admin/login/?next=/admin/ HTTP/1.1" 302 0
    "GET /admin/ HTTP/1.1" 200 3407

    View full-size slide

  10. method="POST"
    enctype="multipart/form-data">



    View full-size slide

  11. method="POST"
    enctype="multipart/form-data">



    View full-size slide

  12. Django Forms!

    View full-size slide

  13. Models
    Database
    Django forms
    HTML forms

    View full-size slide

  14. from django import forms
    class MessageForm(forms.Form):
    name = forms.CharField(
    max_length=100,
    )
    title = forms.CharField(
    max_length=100, required=False,
    )
    content = forms.CharField(
    widget=forms.Textarea,
    )

    View full-size slide

  15. from .forms import MessageForm
    def message_board(request):
    form = MessageForm()
    return render(
    request, 'message_board.html',
    {'form': form},
    )

    View full-size slide


  16. {% csrf_token %}
    {{ form.as_p }}
    ૹग़

    View full-size slide


  17. {% csrf_token %}
    {{ form.as_p }}
    ૹग़

    ?

    View full-size slide

  18. CSRF Token
    • Cross Site Request Forgery
    protection
    • “Recognise” whether a form is legit
    • Protect the server from malicious
    changes

    View full-size slide

  19. class Message(models.Model):
    name = models.CharField(
    max_length=100,
    )
    title = models.CharField(
    max_length=100, blank=True,
    )
    content = models.TextField()
    created_at = models.DateTimeField(
    auto_now_add=True,
    )

    View full-size slide

  20. class Message(models.Model):
    name = models.CharField(
    max_length=100,
    )
    title = models.CharField(
    max_length=100, blank=True,
    )
    content = models.TextField()
    created_at = models.DateTimeField(
    auto_now_add=True,
    )

    View full-size slide

  21. class MessageForm(forms.Form):
    # ...
    def save(self):
    data = self.cleaned_data
    message = Message(
    name=data['name'],
    title=data['title'],
    content=data['content'],
    )
    message.save()
    return message

    View full-size slide

  22. class MessageForm(forms.Form):
    # ...
    def save(self):
    data = self.cleaned_data
    message = Message(
    name=data['name'],
    title=data['title'],
    content=data['content'],
    )
    message.save()
    return message

    View full-size slide

  23. def message_board(request):
    if request.method == 'POST':
    form = MessageForm(request.POST)
    if form.is_valid():
    form.save()
    else:
    form = MessageForm()
    return render(
    request, 'message_board.html',
    {'form': form},
    )

    View full-size slide

  24. Further Reading
    • Model forms
    • Form factories
    • Custom “cleaning”

    View full-size slide

  25. Exercises
    • Implement message form
    • Implement message model,
    save POST-ed form to it
    • Get messages from database
    • Display messages in template

    View full-size slide

  26. Hint
    {% for m in messages %}

    {{ m.name }}
    {{ m.title }}
    {{ m.content|linebreaks }}

    {% endfor %}
    Message.obejcts.order_by('-created_at')

    View full-size slide