Monday, 5 July 2021

*Episode 6* PYTHON (Creating a Django Projects)


Creating a Django project


This episode covers :-


 1.) How to create a new Django project.

 2.) How to use the built-in development server.


  •  Setup :-

Terminal

cd projects

mkdir 08-Django-Project

cd 08-Django-Project

source ../venv/bin/activate


     You don’t have to activate the virtual environment if it’s already activated.


  •  Creating a new Project :-

        Install Django and use the startproject command to create a new Django project:

Terminal

pip install django

django-admin startproject mysite .


       You should now have this kind of folder structure:

Project folder structure

projects

├── 08-Django-Project

│ ├── manage.py

│ └── mysite

└── venv

├── bin

├── include

├── lib

├── pip-selfcheck.json

└── pyvenv.cfg


      08-Django-Project folder is a container for the whole project. The mysite folder inside it is the project Python package that connects your project with Django.


  •  Running the development server :-

      Use runserver to run the server:

Terminal

python manage.py runserver


Visit http://127.0.0.1:8000/ and you should see the welcome screen:



  •  In Details :-

     django-admin is a command-line tool that helps you with management tasks:
Terminal
django-admin startproject mysite .

      startproject command creates the Django project structure. “.” denotes that we want to create the project in the current directory.

      This also creates the manage.py file in the project root. manage.py does the same thing as django-admin plus it takes care of few things for you. For example, before you can use Django, you need to tell it which settings.py file to use. manage.py does this by defining an environment variable with the name
“DJANGO_SETTINGS_MODULE”. You don’t have to worry about this though.
Just use manage.py for administration tasks like this:
Terminal
python manage.py makemigrations


        You might have noticed that a database file was generated in the project root. By default, Django is configured to use the SQLite database. This is perfectly fine for development purposes but for production you should consider other alternatives. With the Heroku platform we use PostgreSQL database.

        You can ignore the “You have 15 unapplied migration(s)..” warning in the terminal. We will deal with migrations and databases later.


Summary


  •  django-admin is a command-line tool for administrative tasks.
  •  startproject command creates a Django project skeleton.
  •  It’s more convenient to use manage.py instead of django-admin for administrative tasks after the project has been created.
  •  SQLite is the default database option but you shouldn’t use it in a production environment.

👈Episode 5(P).                                                                                  Episode 7(P)👉
Share This Post
PRINT THIS POST

No comments:

Post a Comment

If you have any doubts. Please let me know.

Featured post

*Episode 1* MCQ for Govt. Job/ Private Job/ MNCs

  Topic:- One Word Substitution 1) Especially skilled in storytelling  Answer:- Raconteur 2) Fear of loneliness Answer:- Eremophobia  3) Usa...