Вы находитесь на странице: 1из 3

Django Notes

1) Setup the django


Create vitual env => mkvirtualenv (nameofenv)
install django=> pip install django
create project => django-admin startproject sugandhi
Run Server => python manage.py runserver
Web Server => Run the local host 127.0.0.1/8000 on browser

2) Hello World in Django


Restart the Vitual env => workon (nameofenv)
Create new app/folder => python manage.py startapp (nameofapp -> calc)
Create urls.py in calc app/folder
In urls.py of calc write the snippet
a)from django.urls import path
b)from . import views
c)a list urlpatterns = [path('',views.home),name = 'home']
d)create the function home in views.py => see views.py for details

In the urls.py of main folder sugandhi add this home page entry
urlpatterns = [path('',include('calc.urls'))]

3) Django Template Language


How to add a complete webpage in django and make it dynamic => Solution is django
templates
Create a new folder named Templates in the same heirarchy of calc
In this folder add the html page you want to add lets say home.html

In the templates list of settings.py of main folder sugandhi add a new list in dirs
=> [os.path.join(BASE_DIR,'Templates')]
Change the return function of views.py in calc folder from return HttpRes.... to =>
return render(request,'home.html')

Dynamic Content => In the return function of views.py after 'home.html' add the
dictionary => {'name':'Aayush'}
In home.html webpage write => {{name}} in the tag

4) How to link Complete Web Page and django


I have created a base.html page in folder Templates this is the basic file which
links with home.html
In the body of this base.html add the following lines =>
{% block content %} ......code of body......... {% endblock %}
In home.html write {% extends 'base.html' %}
and then same {% block content %} ......code of body......... {% endblock %}

5) Take the Data from the User and make operations on it

In home.html we have created a form which is taking 2 inputs from the user and
calcuating sum of it.
When user clicks on the submit button the request will go on the add(which is the
form action)
This add function is present in the views.py, in this function we are fetching the
values that we get from the user.
This function will return the value of sum to return.html .
Here we have used GET Method
Path is HTML File -> urls.py -> views.py -> html file

6) GET and POST HTTP Methods

Expecting Something from Server => GET Method


Send Something to Server => POST Method

Use csrf token to send the data from client to the Server

6) MVT MODEL VIEW Template

7) Working on a small Project (Travello)

a) Create a folder name travello in the same heirarchy of the main folder sugandhi
Create new app/folder => python manage.py startapp (nameofapp -> travello)
Create urls.py in travello app/folder
In urls.py of travello write the snippet
a)from django.urls import path
b)from . import views
c)urlpatterns = [
path('',views.index,name='index')
]
d)create the function index in views.py => see views.py for details
In settings.py of the main folder bankloan we will find we will find the Installed
Apps named list update this list to

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'BankLoans.apps.BankloansConfig',
]
In the urls.py of main folder sugandhi add this home page entry
urlpatterns = [path('',include('travello.urls'))]
b) Also create a new folder static(which contains all the html css and jss files of
the website) This folder is in the same heirarchy of the travello
c) In settings.py of main folder sugandhi add a new path for the static folder, add
a new list beneath the list STATIC_URL,
STATICFILES_DIRS = [os.path.join(BASE_DIR,'sugandhi/static')]
d) Django wants to put all of these static files in its own folder which is done by
creating a new list
STATIC_ROOT = os.path.join('BASE_DIR','assets')
e) But django should know that he has to create a folder name assets
on cmd type the command python manage.py collectstatic
f) In every line of the html file which contains the href tag, which is the link
either to the css file or to any new page
href = {%static .......original-href-tag.... %} , same for src and styles
plugins,beware of the background image
g) Also, in every html file write {% load static %} on the 1st line

8) Passing Dynamic Data in html


a)Now we create our Models, and make the changes in Models.py
In models.py we will create the class and its objects.
Create one more object for the future use of database
example => price:int

b) In views.py import this model using => from .models import


Destination(className)
Intasiate the object here dest1 = Destination()
call the params of the object dest1.name = 'Mumbai'
In the return function we have to pass only the object
Create list of different objects
dests = [dest1,dest2,dest3]
return render(request,'index.html',{'dests':dests})

c)For loop in django Since we cant do this as no. of destn is also dynamic, we will
create a loop , see in index.html how for loop works

d)But the biggest problem is dynamic addition of images, jinja cant be placed
inside another jinja
To overcome this write {% static "images" as baseUrl%} on the top of the HTML
file
<img src="{{baseUrl}}/{{dest.img}}" alt="">

e) If else in django {% if dest.offer %} .........code .............{% endif %}

Create the folder using python manage.py startapp travello


Copy the urls.py
Get safe from the csrf token using @csrf_exempt over the function which is using
POST command
from django.views.decorators.csrf import csrf_exempt

Вам также может понравиться