PrathicaShettyM / Django-Backend

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Django-Backend

This is a popular python web framework for rapidly building web applications

Table of Contents

First-time Installation

We need uv package manager

  1. Set a virtual environment for the django project with the command
uv venv
  1. Activate the virtual environment
.venv\Scripts\activate
  1. If you face any error in Windows OS, open the windows-powershell as an administrator and type the following command
set-exectionpolicy remotesigned
  1. Install Django with the command
uv pip install Django
  1. Create a new Django project
django-admin startproject MyApp
  1. Come inside the project folder
cd MyApp
  1. Run the Django server
py manage.py runserver 
  • Use the above command whenever you want to restart the server(Dont forget to navigate to the manage.py's directory)

  • If some the port is bloacked for some reason then use a port number

py manage.py runserver 8001

To run subsequent times(After restarting the system)

You will need to use the following commands

 uv venv
.venv\Scripts\activate
  • Note: If you are not restarting the system, then use py manage.py runserver command

Django Architecture

  1. A project with the desired name is created, along a subfolder with the same name which contains - views.py: contains the core functionalities of the application(contains logic) urls.py: contains all routes settings.py: contains all settings

  2. db.sqlite3 is a default database for django which can also be migrated to postgres and mysql

  3. manage.py is the starting point of the application. It is the server

  4. static folder contains css and js files

  5. templates folder contains html files

Setting up static and template folders for use

  1. change this in settings.py to load css and js files
STATIC_URL = 'static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
  1. In index.html do the following changes
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MyApp | Django project</title>
    <link rel="stylesheet" href="{% static 'style.css' %}">
</head>
<body>
    <h1>This is a Django project</h1>
    
</body>
</html>
  1. In views.py do the following to render the html file on the browser
def home(request):
    return render(request, 'website/index.html')

TailwindCSS with Django

  1. Install pip and then tailwind css
  • Whichever the below command works use it
  • command 1
py -m ensurepip --upgrade
  • command 2
py -m pip install --upgrade pip

Note: use both when needed

  1. Install tailwind css with the following command
pip install 'django-tailwind[reload]'
  1. Go to settings.py and in apps object, add 'tailwind'

  2. Use this command to initiate tailwind in the server. Some packages will be installed along with a folder named theme if you keep the default settings

py manage.py tailwind init
  1. Go to settings.py and add 'theme' in the app section
  2. Next below it give the name of the tailwind app and InternalIP
TAILWIND_APP_NAME = 'theme'
INTERNAL_IPS = ['127.0.0.1']
  1. Use the command

About


Languages

Language:Python 77.8%Language:JavaScript 12.8%Language:HTML 7.8%Language:CSS 1.7%