English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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−
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 c:\> django-admin startproject myproject
This will create a 'myproject' folder structure as follows−
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 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, which actually contains two elements −
manage.py - the file is a local django project-admin interacts with the project through the command line (start the development server, synchronize the database...). You can learn about the available code through manage.py −
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 C:> python manage.py help
In the 'myproject' subdirectory - this folder is the actual Python package of the project. It contains four files −
__init__.py - it is only for Python, handling the packages in this folder. settings.py - as the name suggests, it is used for project settings. urls.py - the various stages and functions called during project creation. All the Toc of the project. wsgi.py - if you need to deploy the project on WSGI.
Set up the project
Your project is based in the subfolder myproject/settings.py. Below are some important options that may need to be set -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 DEBUG = True
This option, as you can see above, whether your project is in debug mode or not. Debug mode allows you to get detailed information about errors in the project. Never set it to 'True' for online projects. However, it must be set to 'True' if you want the Django server to handle static files. It usually uses the development mode.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite'3', 'NAME': 'database.sql', 'USER': '', 'PASSWORD': '', 'HOST': '', 'PORT': '', } }
Database settings are in the 'Database' dictionary. The above example is the SQLite engine. As mentioned before, Django also supports−
MySQL (django.db.backends.mysql) PostGreSQL (django.db.backends.postgresql_psycopg)2) Oracle (django.db.backends.oracle) and NoSQL DB MongoDB (django_mongodb_engine)
When setting any new engine, make sure you have installed the correct DB driver.
You can also set other options, such as: TIME_ZONE, LANGUAGE_CODE, TEMPLATE...
Now, your project has been created and configured, make sure it works -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 c:\myproject>', monospace; line-height:1.5">python manage.py runserver
Here is the code to run the above as follows -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 C:\myproject>python manage.py runserver Performing system checks... System check identified no issues (0 silenced). You have unapplied migrations; your app may not work properly until they are applied lied. Run 'python manage.py migrate' to apply them. January 23, 2016 - 09:08:32 Django version 1.9.1, using settings 'myproject.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-break.