This is a popular python web framework for rapidly building web applications
- First time Installation
- Subsequent running
- Django architecture
- Setting up Static and Template folders
- Tailwind CSS with Django
- Django Templates
We need uv
package manager
- Set a virtual environment for the django project with the command
uv venv
- Activate the virtual environment
.venv\Scripts\activate
- If you face any error in Windows OS, open the
windows-powershell
as anadministrator
and type the following command
set-exectionpolicy remotesigned
- Install Django with the command
uv pip install Django
- Create a new Django project
django-admin startproject MyApp
- Come inside the project folder
cd MyApp
- 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
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
-
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 routessettings.py
: contains all settings -
db.sqlite3
is a default database for django which can also be migrated topostgres
andmysql
-
manage.py
is the starting point of the application. It is the server -
static folder
containscss
andjs
files -
templates folder
containshtml
files
- change this in
settings.py
to loadcss
andjs
files
STATIC_URL = 'static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
- 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>
- In
views.py
do the following to render the html file on the browser
def home(request):
return render(request, 'website/index.html')
- Install
pip
and thentailwind 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
- Install
tailwind css
with the following command
pip install 'django-tailwind[reload]'
-
Go to
settings.py
and in apps object, add 'tailwind' -
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
- Go to
settings.py
and add 'theme' in the app section - Next below it give the name of the tailwind app and
InternalIP
TAILWIND_APP_NAME = 'theme'
INTERNAL_IPS = ['127.0.0.1']
- Use the command