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.
4.1 KiB
4.1 KiB
Dear Diary Project
This is notes from course.
Create Project And App
-
Under
undestanding-django
directory, create projectdear_diary
$ pipenv install $ pipenv shell $ django-admin startproject dear_diary
-
Go to
dear_diary
directory and create appentries
$ 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
-
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
-
Create superuser
$ python manage.py createsuperuser Username (leave blank to use 'bobson'): Email address: bobson.lin@aiwill.tech Password: Password (again): Superuser created successfully.
-
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
- Create templates directory for
entries
app - Create functions(index, add) in views.py
- Create entries/urls.py and adjust dear_diary/urls.py
- Add
entries
app in settings.pyINSTALLED_APPS
section
Add Page Link
- Use
name
argument indjango.urls.path
function. - In template file(*.html), add
{% url '[name]' %}
(eg.{% url 'add' %}
for/add
url)
Create Entry Model
- Add Entry model class in models.py
- Register Entry model in admin.py
- 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
- Check http://127.0.0.1:8000/admin
Display Entries From Database
- Query entries in
views.index
function. - Use for loop in
index.html
template.
Creating A Form
- Create
EntryForm
in entries/forms.py. - Use
EntryForm
inviews.add
function. - Use form in
add.html
template.
Processing The Form
- Use
EntryForm
to handle POST request. - Redirect after submit form.
- Add CSRF token in form.