An open API service indexing awesome lists of open source software.

https://github.com/codegeek004/django


https://github.com/codegeek004/django

Last synced: 8 months ago
JSON representation

Awesome Lists containing this project

README

          

Django Tutorial


This is a django application developed from the official documentation's tutorial. This will explain you the flow of django framework.


To start with we have to create a virtual environment for django and access it.

python3 -m venv django-env

source django-env/bin/activate

After creating the virtual environment you need to install django


pip install django

Now you need to bootstrap a django project using the following command


django-admin startproject mysite django_tutorial

Now your directory looks something like this:

django_tutorial/

manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py

In this the manage.py file is used to run the django application(s). The mysite directory is the project. Using this project directory you an create multiple applications.

  • To start with the first file is __ini__.py which indicates that the directory containing this file should be treated as the python project.
  • settings.py file is responsible for defining the configurations for our applications. Key components of settings.py

    • DEBUG=True We can turn on or off the debug mode using this. DEBUG should be set to false when application is in production

      ALLOWED_HOSTS['*'] Using this we can specify what are the host/domain names your application could serve. This prevents HTTPS host head attacks
      INSTALLED_APPS = [
      
      'django.contrib.admin',
      'django.contrib.auth',
      'django.contrib.contenttypes',
      'django.contrib.sessions',
      'django.contrib.messages',
      'django.contrib.staticfiles',
      'polls', # Your app
      ]
      INSTALLED_APPS contains django's built in apps and custom apps you have created.

        DATABASES contains configurations for connecting to your database.

    • urls.py contains path to all your applications in the django project

    • asgi.py is an entry-point for ASGI-compatible web servers to serve your project

    • wsgi.py is an entry-point for WSGI-compatible web servers to serve your project

    • After successfully creating the project you need to make applications. For creating the applications you need to create an app in the same directory.
      python manage.py startapp poll

      This will create a app directory poll which is laid out something like this.

      polls/
      
      __init__.py
      admin.py
      apps.py
      migrations/
      __init__.py
      models.py
      views.py

    • Just like the project directory the __init__.py file here too indicates that this file should be treated as a python package which allows you to import modules from this directory.

    • admin.py file is used for configuring the django admin interface.

    • apps.py file contains the configurations of the application. It defines the app's name and configurations.

    • the migrations directory contains the information about the files migrations which are used to manage changes to your database schema.

    • models.py file is where you define the database models. Each model corresponds to a database table and defines the structure of the data.


    • In views.py file you define the views for your application. Views handle the logic behind what data is displayed and how it is presented to the user.

    • To run your application run the command

      python manage.py runserver
      .
      If you have any changes in your database run the commands
      python manage.py makemigrations
      
      python manage.py migrate


      # django