English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

创建项目 Django

Now that we have installed Django, let's start using it. In Django, each web application to be created is called a project; a project is the sum of applications. An application is a set of code files based on the MVC pattern. As an example, let's build a website, which is our project, with forum, news, and contact applications. This structure makes it easier to move applications between projects, as each application is independent.

Create project

Whether you are on Windows or Linux, just get a terminal or a command prompt and navigate to the location where you want to create the project, and then execute the following code −

# 文件名:example.py
# 版权:2020 By w3codebox
# 作者:it.oldtoolbag.com
# 日期:2020-08-08
c:\> django-admin startproject myproject

This will create a 'myproject' folder structure as follows −

# 文件名:example.py
# 版权:2020 By w3codebox
# 作者:it.oldtoolbag.com
# 日期:2020-08-08
myproject/
    manage.py
    myproject/
       __init__.py
       settings.py
       urls.py
       wsgi.py

Project structure

The 'myproject' folder is just a container for your project, it actually contains two elements −

manage.py − file is a local project file used by django-admin to interact with the project via command line (start development server, synchronize database...). manage.py can be used to understand the available code −

# 文件名:example.py
# 版权:2020 By w3codebox
# 作者:it.oldtoolbag.com
# 日期:2020-08-08
C:> python manage.py help

 Nella sottocartella "myproject" - questa cartella è il pacchetto Python effettivo del progetto. Contiene quattro file -

__init__.py − solo per Python, gestisce il pacchetto di questa cartella.     settings.py − come suggerisce il nome, utilizzato per le impostazioni del progetto.     urls.py − tutti i passaggi e le funzioni da chiamare nella creazione del progetto. Tutti i Toc del progetto.     wsgi.py − se devi distribuire il progetto su WSGI.      

Impostazione del progetto

Il tuo progetto è situato nella sottocartella myproject/settings.py. Di seguito sono riportate alcune opzioni importanti che potresti dover impostare -

# 文件名:example.py
# 版权:2020 By w3codebox
# 作者:it.oldtoolbag.com
# 日期:2020-08-08
DEBUG = True

Questa opzione, come visto sopra, determina se il tuo progetto è in modalità di debug o non lo è. La modalità di debug ti permette di ottenere dettagli dettagliati sugli errori del progetto. Non impostare mai su "True" un progetto online. Tuttavia, deve essere impostato su "True" se si desidera che il server Django gestisca i file statici. Di solito viene utilizzata la modalità di sviluppo.

 # 文件名:example.py
# 版权:2020 By w3codebox
# 作者:it.oldtoolbag.com
# 日期:2020-08-08
DATABASES = {
    'default': {
       'ENGINE': 'django.db.backends.sqlite3',
       'NAME': 'database.sql',
       'USER': '',
       'PASSWORD': '',
       'HOST': '',
       'PORT': '',
    }
 }

Le impostazioni del database si trovano nel dizionario "Database". L'esempio sopra è per il motore SQLite. Come menzionato in precedenza, Django supporta anche -

MySQL (django.db.backends.mysql) PostGreSQL (django.db.backends.postgresql_psycopg2) Oracle (django.db.backends.oracle) e NoSQL DB MongoDB (django_mongodb_engine)

Prima di impostare qualsiasi nuovo motore, assicurati di aver installato il corretto driver DB.

Puoi anche impostare altre opzioni come: TIME_ZONE, LANGUAGE_CODE, TEMPLATE...

Ora, assicurati che la creazione e configurazione del tuo progetto funzionino correttamente -

# 文件名:example.py
# 版权:2020 By w3codebox
# 作者:it.oldtoolbag.com
# 日期:2020-08-08
c:\myproject> ', monospace;line-height:1.5;">python manage.py runserver

这里将获得像上运行上面的代码如下 -

# 文件名:example.py
# 版权:2020 By w3codebox
# 作者:it.oldtoolbag.com
# 日期:2020-08-08
C:\myproject>python manage.py runserver
 正在执行系统检查...
 系统检查未发现任何问题(0 已静音)。
 您有未应用的迁移;直到它们被应用,您的应用程序可能无法正常工作。
 lied.
 运行 'python manage.py migrate' 以应用它们。
 2016年1月23日 - 09:08:32
 版本 Django 1.9.1,使用设置 'myproject.settings'
 在 http://127.0.0.1:8000/ 启动开发服务器。 
 使用 CTRL-break 退出服务器。