Browse Source

feat: Dear Diary Project - 8. Creating A Form

master
Bobson Lin 2 years ago
parent
commit
5492c39561
6 changed files with 88 additions and 17 deletions
  1. +2
    -0
      Pipfile
  2. +46
    -2
      Pipfile.lock
  3. +8
    -2
      dear_diary/README.md
  4. +13
    -0
      dear_diary/entries/forms.py
  5. +11
    -10
      dear_diary/entries/templates/entries/add.html
  6. +8
    -3
      dear_diary/entries/views.py

+ 2
- 0
Pipfile View File

@ -7,6 +7,8 @@ name = "pypi"
django = "*"
[dev-packages]
"flake8" = "*"
"autopep8" = "*"
[requires]
python_version = "3.6"

+ 46
- 2
Pipfile.lock View File

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "68309cd71a258c30a39567fce09a09ad5c4ff0bdc85b6fba22b47598c985c883"
"sha256": "ac80cd7a62ad7720c63e4571827769d9472aff77de4fa2de55b24881ac1b0451"
},
"pipfile-spec": 6,
"requires": {
@ -39,5 +39,49 @@
"version": "==0.3.0"
}
},
"develop": {}
"develop": {
"autopep8": {
"hashes": [
"sha256:4d8eec30cc81bc5617dbf1218201d770dc35629363547f17577c61683ccfb3ee"
],
"index": "pypi",
"version": "==1.4.4"
},
"entrypoints": {
"hashes": [
"sha256:589f874b313739ad35be6e0cd7efde2a4e9b6fea91edcc34e58ecbb8dbe56d19",
"sha256:c70dd71abe5a8c85e55e12c19bd91ccfeec11a6e99044204511f9ed547d48451"
],
"version": "==0.3"
},
"flake8": {
"hashes": [
"sha256:859996073f341f2670741b51ec1e67a01da142831aa1fdc6242dbf88dffbe661",
"sha256:a796a115208f5c03b18f332f7c11729812c8c3ded6c46319c59b53efd3819da8"
],
"index": "pypi",
"version": "==3.7.7"
},
"mccabe": {
"hashes": [
"sha256:ab8a6258860da4b6677da4bd2fe5dc2c659cff31b3ee4f7f5d64e79735b80d42",
"sha256:dd8d182285a0fe56bace7f45b5e7d1a6ebcbf524e8f3bd87eb0f125271b8831f"
],
"version": "==0.6.1"
},
"pycodestyle": {
"hashes": [
"sha256:95a2219d12372f05704562a14ec30bc76b05a5b297b21a5dfe3f6fac3491ae56",
"sha256:e40a936c9a450ad81df37f549d676d127b1b66000a6c500caa2b085bc0ca976c"
],
"version": "==2.5.0"
},
"pyflakes": {
"hashes": [
"sha256:17dbeb2e3f4d772725c777fabc446d5634d1038f234e77343108ce445ea69ce0",
"sha256:d976835886f8c5b31d47970ed689944a0262b5f3afa00a5a7b4dc81e5449f8a2"
],
"version": "==2.1.1"
}
}
}

+ 8
- 2
dear_diary/README.md View File

@ -120,6 +120,12 @@ Create Entry Model
Display Entries From Database
-----
1. Query entries in views.index function.
2. Use for loop in index.html template.
1. Query entries in `views.index` function.
2. Use for loop in `index.html` template.
Creating A Form
-----
1. Create `EntryForm` in [entries/forms.py](./entries/forms.py).
2. Use `EntryForm` in `views.add` function.
3. Use form in `add.html` template.

+ 13
- 0
dear_diary/entries/forms.py View File

@ -0,0 +1,13 @@
from django.forms import ModelForm
from .models import Entry
class EntryForm(ModelForm):
class Meta:
model = Entry
fields = ('text', )
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['text'].widget.attrs.update(
{'class': 'textarea', 'placeholder': 'What\'s on your mind?'})

+ 11
- 10
dear_diary/entries/templates/entries/add.html View File

@ -22,18 +22,19 @@
<section class="section">
<div class="container">
<div class="field">
<div class="control">
<textarea class="textarea" placeholder="What's on your mind?"></textarea>
<form action="{% url 'add' %}" method="post">
<div class="field">
<div class="control">
{{ form.text }}
</div>
</div>
</div>
<div class="field is-grouped is-grouped-right">
<div class="control">
<button class="button is-link">Submit</button>
<div class="field is-grouped is-grouped-right">
<div class="control">
<button class="button is-link">Submit</button>
</div>
</div>
</div>
</form>
</div>


+ 8
- 3
dear_diary/entries/views.py View File

@ -1,13 +1,18 @@
from django.shortcuts import render
from .models import Entry
from .forms import EntryForm
def index(request):
entries = Entry.objects.all()
# entries = Entry.objects.all()
entries = Entry.objects.order_by('-date')
context = { 'entries': entries }
context = {'entries': entries}
return render(request, 'entries/index.html', context)
def add(request):
return render(request, 'entries/add.html')
form = EntryForm()
context = {'form': form}
return render(request, 'entries/add.html', context)

Loading…
Cancel
Save