Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

how to create django project

--------------------------------------------
pip version to install necessary libraries upgrade python -m pip install --upgrade
pip
install django
check the version

to create django project


type --> django-admin startproject firstproject

then immediately below mentioned py will be created


firstproject
firstproject
__init__.py
settings.py ***
urls.py ***
wsgi.py

to create app django-admin manage.py startapp testapp

init.py ----> python package will be empty by default


why is require : is consider as python package
settings.py ---> to make the settings for python
all settings for projects are available in this file
urls.py --- > contains all your URL links - login, inbox compose page
wsgi.py ---> webserver gateway interface. this file will be use for production
environment or live

manage.py to runserver , to create apps (main file)

in a project we can take any number of application

what is server its provides the envionment to run the apps.


django provides inbuilts server

one in build db sqlite


can i configure my own db - yes, mysql

can i configure our own development server

Start Application
python maange.py startapp application name
testapp
__init__.py
views.py is a function is alway going to take the HTTP response
request --- response
write business logic in this file (function based , class based)
models.py
admin.py
app.py
test.py
folder - migrations empty folder

review:
django-admin startproject project-name
default files created

to move to firstproject folder at the command line


create application : python manage.py startapp application name : testapp
---:folder
this testapp will contains 6 (six) .py files and default folder migrations

how the project works

start project
start application
add this application to the project in settings.py file - project level
define view functions inside views.py
define url pattern for our view function inside urls.py
runserver
send request

http://127.0.0.1:8000/hello/

Single application with multiple views

1. start-project django-admin startproject thirdproject


2. start application python manage.py startapp testapp
3. add application to the project in side settings.py
4. define view functions inside views.py
5 define urls pattern
6. run server
7. send request

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^date/', views.hello_world_view),
url('morning/', views.good_morning_view),
url('afternoon/',views.good_afternoon_view),
url('evening/', views.good_evening_view),

1. start-project django-admin startproject thirdproject


2. start application python manage.py startapp testapp
3. create template folder in our main project folder
4. add application to the project in side settings.py
add templates folder path inside settings.py
5. define view functions inside views.py
6 define urls pattern
7. run server
8. send request

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR=os.path.join(BASE_DIR,'templates') --- pointing to template

templates
python business and html related presentation logic

Django Template Based Application


1. start project
2. start application
3.create a folder templates , create testapp folder, define all required template
files(html files)
4. add application ub the Settings.py, add template folder path to settings.py
5. define business logic inside views.py
*** from django.shortcuts import render
function render to use in app

dict
def template_view(request):
dt=datetime.datetime.now()
my_dict={'date':dt}
return render(request,'testapp/results.html',context=my_dict)

or

def template_view(request):
dt=datetime.datetime.now()
return render(request,'testapp/results.html',{'date':dt})

You might also like