Jun 22, 2024
conda create -n django_qs python=3.9
conda activate django_qs
conda install -c anaconda django # or pip install django
mkdir django_qs
cd django_qs
django-admin startproject myproject
cd myproject
python manage.py runserver
localhost:8000
(default) or specify a port python manage.py runserver 8080
.Ctrl + C
python manage.py startapp myapp
index.html
):
mkdir myapp/templates
echo "<p>Can you read this?</p>" > myapp/templates/index.html
settings.py
to include template directory path:
import os
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'myapp/templates')],
...
},
]
views.py
to render the template:
from django.shortcuts import render
def myview(request):
return render(request, 'index.html')
urls.py
in the app and map the view function:
from django.urls import path
from . import views
urlpatterns = [
path('myview/', views.myview, name='myview'),
]
myproject/urls.py
:
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('myapp/', include('myapp.urls')),
]
mkdir myapp/static
mkdir myapp/static/css
echo "*{background-color: slategray; text-align: center; color: white;}" > myapp/static/css/style.css
settings.py
to include the static directory path:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'myapp/static')
]
<!DOCTYPE html>
<html>
<head>
<title>First Django App</title>
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">
</head>
<body>
<p>Can you read this?</p>
</body>
</html>
mkdir myapp/static/js
echo "console.log('yoyoyo');" > myapp/static/js/script.js
<script type="text/javascript" src="{% static 'js/script.js' %}"></script>
python manage.py runserver
localhost:8000/myapp/myview
myapp/urls.py
path to empty string and rerun the server.