django notes

Solutions on MaxInterview for django notes by the best coders in the world

showing results for - "django notes"
Filippo
07 Mar 2016
1jupyter notebook
2my django project
3
4
5admin styles
6app.js in static folder have absolute urls
7
8    cd f:/Dev/Python/menab_cms/env/Scripts
9    activate
10    cd f:/Dev/Python/menab_cms
11    python manage.py runserver
12
13## DJANGO NOTES 
14    django web application framework designed as MTV(model template view)
15        - view contain all the business logics
16        - model works with the database structure
17        - template  is work with layout of the application
18    DTL - django Template language
19
20# Prepare virtual environment
21    python -m venv env
22    cd env/Scripts
23    run activate.bat
24
25# install from requirements file
26    pip install -r requirements.txt
27
28# get current dependencyLib
29    pip freeze > requirements.txt
30
31# Setup django project
32    pip install django
33    pip install psycopg2
34    python -m pip install Pillow
35    pip install django-ckeditor
36
37# Sample urls.py file
38    from django.urls import path
39    from . import views
40    urlpatterns = [
41        path('url_part', views.class_name.method_name, name=name),
42    ]
43
44    Main urls.py sample
45
46    from django.contrib import admin
47    from django.urls import path, include
48
49    urlpatterns = [
50        path('calc/' , include('app_name.urls'))
51    ]
52
53# Sample view file
54
55    from django.shortcuts import render
56    from django.http import HttpResponse
57    from django.shortcuts import render
58
59    class Calculator:
60        def index(request):
61            return HttpResponse('this is a sample index')
62        
63        def home(request):
64            context = {'name': 'Girma'}
65            return render(request,'calc/index.html', context)
66
67# Add templates folder
68     {{ name }} -- access values
69     {% block content %}
70
71     {% end block%}
72
73     {% extends file_name %} -- to insert current block to a parent file
74
75- create a folder in the root directory of current project
76    - create folder with name of the app(optional)
77- add your template folder name to TEMPLATES->DIRS of settings.py
78    - 'DIRS': [os.path.join(BASE_DIR, 'templates')],
79- creat your html file
80
81# Static files
82- create a folder into he root directory to store all of static files
83
84STEPS 
85
86    STATICFILES_DIRS = (
87        os.path.join(BASE_DIR, 'static'),
88    )
89    STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
90
91    - django moves all the static files to assets folder when 
92      python manage.py collect static command is run
93    - add {% load static %} to the template top section
94    - access file as {% static "--location and d file name in static folder--" %}
95
96
97
98
99# Connect to database
100    DATABASES = {
101        'default': {
102            'ENGINE': 'django.db.backends.postgresql',
103            'NAME': '',
104            'HOST': 'localhost',
105            'USER': '',
106            'PASSWORD': ''
107        }
108    }
109# Create and check migration
110    python manage.py makemigrations --app_name--
111    python manage.py sqlmigrate --app_name-- --migration-name--
112    python manage.py migrate
113
114
115# Basic commands
116
117- django-admin startproject projectName -- create new project
118- django-admin startapp appName -- add new app to a project
119- python manage.py runserver -- lounch a server for the current project
120- python manage.py createsuperuser
121
122