Tuesday, March 15, 2022

Setup Fresh Python Django Project In Ubuntu Linux

Python is a computer programming language. often used to build websites and software, automate tasks, and conduct data analysis. 

It is used for:-

web development (server-side),
software development,
system scripting,
automate tasks & conduct data analysis.

Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. Built by experienced developers, Django takes care of much of the hassle of web development.


Software Requirment:

Nginx Webserver 
Postgres Database
Python3, Django & thier Dependensis


Step 1) Install Required Software

  •  Install Python

$ sudo apt update
$ sudo apt install software-properties-common -y
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt update
$ sudo apt install python3.8 -y
$ python3 --version


Step 2) Install Django & Dependencies

$ sudo apt install python3-pip
$ sudo apt-get install python3.8-venv -y
$ sudo apt-get install python3-django -y


Step 3) Prepare Your Virtual Environment

$ cd /var/www/html/
$ sudo python3 -m venv env
$ source env/bin/activate

$ python3 -m pip install django
$ python -m pip freeze > requirements.txt

  •  Install & Update All Packages & Dependencies Via Single File Every Time.

$ python -m pip install -r requirements.txt


Step 4) Set Up a Django Project

$ django-admin startproject myproject

Running this command creates a default folder structure,
which includes some Python files and your management app that has the same name as your project:-


In the above picture, you can see the folder structure that the startproject command created for you:

  • myproject/ is your top-level project folder.
  • myproject/myproject/ is your lower-level folder that represents your management app.
  • manage.py is a Python file that serves as the command center of your project. It does the same as the django-admin command-line utility.

The myproject/myproject/ folder contains files that you’ll edit when you work on your web application.


Step 5) Start a Django App

$ cd /var/www/html/myproject

  •  This Command Will Run The Project On Python Default Port 8000

$ python manage.py runserver

  •  Run With A Specific Port Number

$ python manage.py runserver 4022


  • Now check In Browser

http://127.0.0.1:4022



Few Basic Commands Below:


No.
     Description                          Command
1 Set up a virtual environment               python -m venv env
2 Activate the virtual environment source env/bin/activate
3 De-activate the virtual environment    deactivate
4 Install Django                                     python -m pip install django
5 Pin your dependencies python -m pip freeze > requirements.txt
6 Set up a Django project django-admin startproject <projectname>
7 Start a Django app python manage.py startapp <appname>




No comments:

Post a Comment

testing