This repository has been archived by the owner on Aug 13, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
151 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
docs/python/django/forms/_posts/2016-07-29-how-to-create-a-form-in-django.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
title: | ||
subtitle: Typical workflow for creating a Django web form | ||
description: Django simplify and automates the process to create a form in simple steps. | ||
layout: post | ||
--- | ||
|
||
## Overview | ||
|
||
A tipical workflow when creating a form in Django consists of these three steps: | ||
|
||
<div class="mermaid"> | ||
graph TB | ||
a[Create a Form class that defines its fields]-->b | ||
b[Create the view to display the form and process received data]-->c | ||
c[create the template to show the form] | ||
</div> | ||
|
||
## Creating the form | ||
|
||
A _form_ can be defined [from scratch](https://docs.djangoproject.com/en/1.9/topics/forms/) | ||
or take the field defitions from [Model](https://docs.djangoproject.com/en/1.9/topics/forms/modelforms/) classes. | ||
|
||
<div class="mermaid"> | ||
graph TB | ||
form-->has_model{"from a model"} | ||
has_model-- no -->scratch["Inherits from django.forms.Form"] | ||
has_model-- yes -->model["Inherits from django.forms.ModelForm"] | ||
scratch-->fields["Define fields using django.forms.*Field"] | ||
</div> | ||
|
||
### New Form | ||
|
||
~~~ python | ||
# forms.py | ||
from django import forms | ||
|
||
class ContactForm(forms.Form): | ||
name = forms.CharField(label='Full name', max_length=70) | ||
~~~ | ||
|
||
### From Model | ||
|
||
~~~ python | ||
from django.forms import ModelForm | ||
from myapp.models import News | ||
|
||
class ArticleForm(ModelForm): | ||
class Meta: | ||
model = News | ||
fields = ['title', 'content'] | ||
~~~ | ||
|
||
Creating an empty form to add an article. | ||
|
||
~~~ python | ||
form = ArticleForm() | ||
~~~ | ||
|
||
Creating a form to change an existing article. | ||
|
||
~~~ python | ||
article = Article.objects.get(pk=1) | ||
form = ArticleForm(instance=article) | ||
~~~ | ||
|
||
|
||
## References | ||
|
||
- Forms API <https://docs.djangoproject.com/en/1.9/ref/forms/api/#module-django.forms> | ||
- Forms tutorial <https://docs.djangoproject.com/en/1.9/topics/forms/> | ||
- New Forms <https://docs.djangoproject.com/en/1.9/topics/forms/> | ||
- Forms from models <https://docs.djangoproject.com/en/1.9/topics/forms/modelforms/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
title: Ruby on Rails | ||
subtitle: | ||
layout: category_posts | ||
--- |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
title: Web | ||
subtitle: | ||
layout: category_posts | ||
--- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
title: Structured Data Markups | ||
subtitle: | ||
layout: category_posts | ||
--- | ||
|
||
## Links | ||
|
||
- JSON-LD <http://json-ld.org/> | ||
- Microdata <https://www.w3.org/TR/microdata/> | ||
- RDFa <https://rdfa.info/> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters