You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Bobson Lin 5492c39561 feat: Dear Diary Project - 8. Creating A Form 5 years ago
..
dear_diary feat: Dear Diary Project - 4 5 years ago
entries feat: Dear Diary Project - 8. Creating A Form 5 years ago
README.md feat: Dear Diary Project - 8. Creating A Form 5 years ago
manage.py feat: Dear Diary Project - 1 5 years ago

README.md

Dear Diary Project

This is notes from course.

Create Project And App

  1. Under undestanding-django directory, create project dear_diary

    $ pipenv install
    $ pipenv shell
    $ django-admin startproject dear_diary 
    
  2. Go to dear_diary directory and create app entries

    $ cd dear_diary
    $ python manage.py startapp entries
    

App Files

  • migrations - For database versioning.
  • admin.py - Register models appearing at admin dashboard.
  • models.py - Control whats save in your database.
  • tests.py - Tests for this app
  • views.py - (Render views/Response) for url

Admin Dashboard

  1. Initialize database (Default generate db.sqlite3)

    $ python manage.py migrate
    
    Operations to perform:
        Apply all migrations: admin, auth, contenttypes, sessions
        Running migrations:
        Applying contenttypes.0001_initial... OK
        Applying auth.0001_initial... OK
        Applying admin.0001_initial... OK
        Applying admin.0002_logentry_remove_auto_add... OK
        Applying admin.0003_logentry_add_action_flag_choices... OK
        Applying contenttypes.0002_remove_content_type_name... OK
        Applying auth.0002_alter_permission_name_max_length... OK
        Applying auth.0003_alter_user_email_max_length... OK
        Applying auth.0004_alter_user_username_opts... OK
        Applying auth.0005_alter_user_last_login_null... OK
        Applying auth.0006_require_contenttypes_0002... OK
        Applying auth.0007_alter_validators_add_error_messages... OK
        Applying auth.0008_alter_user_username_max_length... OK
        Applying auth.0009_alter_user_last_name_max_length... OK
        Applying auth.0010_alter_group_name_max_length... OK
        Applying auth.0011_update_proxy_permissions... OK
        Applying sessions.0001_initial... OK
    
  2. Create superuser

    $ python manage.py createsuperuser                                              
    Username (leave blank to use 'bobson'): 
    Email address: bobson.lin@aiwill.tech
    Password: 
    Password (again): 
    Superuser created successfully.
    
  3. Run project (http://127.0.0.1:8000/admin will show admin dashboard)

    $ python manage.py runserver
    
    Watching for file changes with StatReloader
    Performing system checks...
    
    System check identified no issues (0 silenced).
    June 21, 2019 - 03:24:05
    Django version 2.2.2, using settings 'dear_diary.settings'
    Starting development server at http://127.0.0.1:8000/
    Quit the server with CONTROL-C.
    

Display Templates

  1. Create templates directory for entries app
  2. Create functions(index, add) in views.py
  3. Create entries/urls.py and adjust dear_diary/urls.py
  4. Add entries app in settings.py INSTALLED_APPS section
  1. Use name argument in django.urls.path function.
  2. In template file(*.html), add {% url '[name]' %} (eg. {% url 'add' %} for /add url)

Create Entry Model

  1. Add Entry model class in models.py
  2. Register Entry model in admin.py
  3. Run command to create migration file and do migration.
    $ python manage.py makemigrations
    Migrations for 'entries':
    entries/migrations/0001_initial.py
        - Create model Entry
    
    $ python manage.py migrate
    Operations to perform:
    Apply all migrations: admin, auth, contenttypes, entries, sessions
    Running migrations:
    Applying entries.0001_initial... OK
    
  4. Check http://127.0.0.1:8000/admin

Display Entries From Database

  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.
  2. Use EntryForm in views.add function.
  3. Use form in add.html template.