Django 基本教學-課程筆記資料分享

 

基本Django poll App 步驟


python -m django --version

django-admin startproject mysite
python manage.py runserver
python manage.py runserver 8080
python manage.py runserver 0:8000
The development server automatically reloads Python code for each request as needed. You don’t need to restart the server for code changes to take effect. However, some actions like adding files don’t trigger a restart, so you’ll have to restart the server in these cases.
A project is a collection of configuration and apps for a particular website.
An app is a Web application that does something – e.g., a Weblog system,

      task:看一下說明檔案

Q: asgi wsgi 

The Web Server Gateway Interface (WSGI, pronounced whiskey or WIZ-ghee) is a simple calling convention for web servers to forward requests to web applications

ASGI is a spiritual successor to WSGI, the long-standing Python standard for compatibility between web servers, frameworks, and applications.

 manage.py   (注意一下mysite-) 

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

 

python manage.py startapp polls

 




Some of these applications make use of at least one database table, though, so we need to create the tables in the database before we can use them. To do that, run the following command:

python manage.py migrate

  python manage.py runserver

 

 http://localhost:8000/polls/

恭喜,你成功的完成Django 網站程式 

基本Django Settings





 




如果使用其他database 要如何做?

your database connection settings:

  • ENGINE – Either 'django.db.backends.sqlite3', 'django.db.backends.postgresql', 'django.db.backends.mysql', or 'django.db.backends.oracle'. Other backends are also available.
  • NAME – The name of your database. If you’re using SQLite, the database will be a file on your computer; in that case, NAME should be the full absolute path, including filename, of that file. The default value, BASE_DIR / 'db.sqlite3', will store the file in your project directory.

If you are not using SQLite as your database, additional settings such as USER, PASSWORD, and HOST must be added. For more details, see the reference documentation for DATABASES.


建立 polls 網頁 


Activating models

That small bit of model code gives Django a lot of information. With it, Django is able to:

  • Create a database schema (CREATE TABLE statements) for this app.
  • Create a Python database-access API for accessing Question and Choice objects.

But first we need to tell our project that the polls app is installed. 

The PollsConfig class is in the polls/apps.py file, so its dotted path is 'polls.apps.PollsConfig'.


有App 後才會有設定好db ,這時可以轉移網站。

python manage.py makemigrations polls


python manage.py sqlmigrate polls 0001


管理資料庫

 

python manage.py shell



For more information on model relations, see Accessing related objects. For more on how to use double underscores to perform field lookups via the API, see Field lookups. For full details on the database API, see our Database API reference.     


留言

這個網誌中的熱門文章

讀書- Input 最高學習法: 激發最大學習效率的輸入大全

2023 Plan