django notes
jupyter notebook
my django project
admin styles
app.js in static folder have absolute urls
cd f:/Dev/Python/menab_cms/env/Scripts
activate
cd f:/Dev/Python/menab_cms
python manage.py runserver
## DJANGO NOTES
django web application framework designed as MTV(model template view)
- view contain all the business logics
- model works with the database structure
- template is work with layout of the application
DTL - django Template language
# Prepare virtual environment
python -m venv env
cd env/Scripts
run activate.bat
# install from requirements file
pip install -r requirements.txt
# get current dependencyLib
pip freeze > requirements.txt
# Setup django project
pip install django
pip install psycopg2
python -m pip install Pillow
pip install django-ckeditor
# Sample urls.py file
from django.urls import path
from . import views
urlpatterns = [
path('url_part', views.class_name.method_name, name=name),
]
Main urls.py sample
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('calc/' , include('app_name.urls'))
]
# Sample view file
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render
class Calculator:
def index(request):
return HttpResponse('this is a sample index')
def home(request):
context = {'name': 'Girma'}
return render(request,'calc/index.html', context)
# Add templates folder
{{ name }} -- access values
{% block content %}
{% end block%}
{% extends file_name %} -- to insert current block to a parent file
- create a folder in the root directory of current project
- create folder with name of the app(optional)
- add your template folder name to TEMPLATES->DIRS of settings.py
- 'DIRS': [os.path.join(BASE_DIR, 'templates')],
- creat your html file
# Static files
- create a folder into he root directory to store all of static files
STEPS
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
- django moves all the static files to assets folder when
python manage.py collect static command is run
- add {% load static %} to the template top section
- access file as {% static "--location and d file name in static folder--" %}
# Connect to database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '',
'HOST': 'localhost',
'USER': '',
'PASSWORD': ''
}
}
# Create and check migration
python manage.py makemigrations --app_name--
python manage.py sqlmigrate --app_name-- --migration-name--
python manage.py migrate
# Basic commands
- django-admin startproject projectName -- create new project
- django-admin startapp appName -- add new app to a project
- python manage.py runserver -- lounch a server for the current project
- python manage.py createsuperuser