「Django」part1.2: Official Docs Note(EN)
關於 Django 的官方文件筆記(EN),紀錄起來,讓自己可以快速統整 👐
< Intro >
"A popular python web framework ! " ----> Django Docs
< Quick Start >
|
|
1. Create Django Project
django-admin startproject mysite
2. Run locally
http://127.0.0.1:8000/
cd mysite
python manage.py runserver
- Changing the port
|
|
|
|
< Create App >
1. Create App polls
Under the same path with
manage.py
python manage.py startapp polls
2. Write first View in App
In the
polls/views.py
|
|
3. Create URLconf to configure urlpatterns in polls
- Under
polls/
- Create
urls.py
In the
polls/urls.py
|
|
4. Point the root URLconf at polls.urls
module ( Setting in mysite/urls.py
)
In the
mysite/urls.py
|
|
5. Run locally to see polls/
http://localhost:8000/polls/
python manage.py runserver
< DB Setup >
- Creates db table
python manage.py migrate
This command looks at the
INSTALLED_APPS
setting and creates any necessary database tables according to the database settings inmysite/settings.py
and the database migrations shipped with the app.
- Creating models
In the
polls/models.py
|
|
- Activating models
We need to include app in project, so we need to reference to its configuration class in the
mysite/settings.py
|
|
But why is polls.apps.PollsConfig
, it's because polls configuration file is in polls/apps.py
and the Class is called PollsConfig
, so we need to write polls.apps.PollsConfig
.
- Make migrations
python manage.py makemigrations polls
- Telling Django that you’ve made some changes to your models and that you’d like the changes to be stored as a migration.
|
|
|
|
- Get the Raw SQL for the migration you give.
python manage.py sqlmigrate polls 0001
- Check for any problems in project
python manage.py check
- Create model tables in daatbase:
So run migrate again.
python manage.py migrate
< So, Basic Migrate Workflow is:>
- Change models in
models.py
- Activate models in INSTALLED_APPS
python manage.py makemigrations
to create migrations for new changes.python manage.py migrate
to apply changes to the database.
< Django python shell >
- Run this command to enter:
|
|
- Can use django framework to edit project, add some change in database here.
|
|
Because usually we don't want
Question.objects.all()
to show<QuerySet [<Question: Question object (1)>]>
for us, we want more clear infos for the model object we choose, so we can add__str__()
in models.py class
- Adding __str__()
method to both Question and Choice in polls/models.py
|
|
- Testing and playing around with shell again:
|
|
|
|
So basically, we can use
python manage.py shell
to write some python code and also Django framework syntex.
< Login to Django admin >
- Creating an admin user
python manage.py createsuperuser
Username: admin
Email address: xxxxxx@xxxx
Password: **********
Password (again): *********
Superuser created successfully.
python manage.py runserver
Go to http://127.0.0.1:8000/admin/
And enter username and password.
Will see the Django administration
- Register our own models in Django admin interface.
- In the
polls/admin.py
from django.contrib import admin
# Register your models here.
from .models import Question, Choice
admin.site.register(Question)
admin.site.register(Choice)
< The End >:
Review:
- "Project" and "App".
- Remember Migration Workflow.
- Using django python shell.
- Create user for django admin interface and login.