Project Directory Structure and All File Information
Project Directory Structure and All File Information
wsgi.py – WSGI (Web Server Gateway Interface) is a specification that describes how a web server
communicates with web applications, and how web applications can be chained together to process one request.
WSGI provided a standard for synchronous Python apps.
asgi.py – ASGI (Asynchronous Server Gateway Interface) is a spiritual successor to WSGI, intended to provide a
standard interface between async-capable Python web servers, frameworks, and applications. ASGI provides
standard for both asynchronous and synchronous apps.
settings.py – This file contains all the information or data about project settings.
E.g.:- Database Config information, Template, Installed Application, Validators etc.
manage.py – manage.py is automatically created in each Django project. It is Django’s command-line utility also
sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.
Generally, when working on a single Django project, it’s easier to use manage.py than django-admin.
Django Project Directory Structure
__init__.py
The folder which contains __init__.py file is considered as python package.
wsgi.py
Importing os module
Function
import os Package Module
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'geekyshows.settings’)
Object callable
This function returns WSGI callable
wsgi.py
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'geekyshows.settings’)
When the WSGI server loads your application, Django needs to import the settings module, that’s where your
entire application is defined.
Django uses the DJANGO_SETTINGS_MODULE environment variable to locate the appropriate settings
module.
It must contain the dotted path to the settings module.
You can use a different value for development and production; it all depends on how you organize your settings.
If this variable isn’t set, the default wsgi.py sets it to mysite.settings, where mysite is the name of your project.
That’s how runserver discovers the default settings file by default.
asgi.py
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'geekyshows.settings’)
application = get_asgi_application()
settings.py
import os
Importing os module
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR is a variable which contains abspath of base directory/project folder
e.g. - C:\AllDjango\geekyshows
settings.py
SECRET_KEY = '9f=3m6^04@*z1dn*!utxe^yn!3vpkjtbbg0&t^+_)cxaigm*7p’
This is used to provide cryptographic signing, and should be set to a unique, unpredictable value.
django-admin startproject automatically adds a randomly-generated SECRET_KEY to each new
project.
Django will refuse to start if SECRET_KEY is not set.
The full Python path of the WSGI application object that Django’s built-in servers (e.g. runserver) will use.
The django-admin startproject management command will create a standard wsgi.py file with an application
callable in it, and point this setting to that application.
If not set, the return value of django.core.wsgi.get_wsgi_application() will be used.
settings.py DATABASES = {
DATABASES = { 'default': {
'default': { 'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'ENGINE': 'django.db.backends.sqlite3', 'USER': 'mydatabaseuser',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 'PASSWORD': 'mypassword',
} 'HOST': '127.0.0.1',
'PORT': '5432',
} }
}
A dictionary containing the settings for all databases to be used with Django.
It is a nested dictionary whose contents map a database alias to a dictionary containing the options for an
individual database.
The DATABASES setting must configure a default database; any number of additional databases may also be
specified.
settings.py
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
The list of validators that are used to check the strength of user’s passwords.
settings.py
LANGUAGE_CODE = 'en-us’
A string representing the language code for this installation. This should be in standard language ID format. For
example, U.S. English is "en-us".
TIME_ZONE = 'UTC’
A string representing the time zone for this installation.
USE_I18N = True
A boolean that specifies whether Django’s translation system should be enabled. This provides a way to turn it
off, for performance. If this is set to False, Django will make some optimizations so as not to load the
translation machinery.
USE_L10N = True
A boolean that specifies if localized formatting of data will be enabled by default or not. If this is set to True,
e.g. Django will display numbers and dates using the format of the current locale.
settings.py
USE_TZ = True
A boolean that specifies if datetimes will be timezone-aware by default or not. If this is set to True, Django will
use timezone-aware datetimes internally. Otherwise, Django will use naive datetimes in local time.
STATIC_URL = '/static/’
URL to use when referring to static files located in STATIC_ROOT.
Example: "/static/" or "http://static.example.com/"
If not None, this will be used as the base path for asset definitions (the Media class) and the staticfiles app.
It must end in a slash if set to a non-empty value.
You may need to configure these files to be served in development and will definitely need to do so in
production.
urls.py
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
manage.py
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'geekyshows.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()