python django - dbgyan.files.wordpress.com · deepak bhinde pgt computer science python django 1.0...

10
2019 PYTHON DJANGO 1.0 DEEPAK BHINDE PGT COMPUTER SCIENCE www.dbgyan.wordpress.com

Upload: others

Post on 22-Sep-2019

39 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: PYTHON DJANGO - dbgyan.files.wordpress.com · DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 2 No. Task 1. Django g et started. 2. Django Architecture

2019

PYTHON DJANGO 1.0

DEEPAK BHINDE

PGT COMPUTER SCIENCE

www.dbgyan.wordpress.com

Page 2: PYTHON DJANGO - dbgyan.files.wordpress.com · DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 2 No. Task 1. Django g et started. 2. Django Architecture

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 2

No. Task 1. Django get started. 2. Django Architecture

3. Django - Creating a Project 4. Create an Application 5. Creating a views 6. URL mapping

7. Start the server

Task points

Page 3: PYTHON DJANGO - dbgyan.files.wordpress.com · DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 2 No. Task 1. Django g et started. 2. Django Architecture

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 3

Django Installation

The process of installing Django on computer system. Just follow the below steps: Step 1: Go to the link: https://www.djangoproject.com/download/

Step 2: Type the pip command on command prompt and installation will get started.

Step: 01

Django get started.

Download:

Django is available open-source under the BSD license:

https://www.djangoproject.com/download/

Download the latest version of Python: https://www.python.org/downloads/

Download python and pip Django latest version and installed on computer

Step 1

Step 2

Page 4: PYTHON DJANGO - dbgyan.files.wordpress.com · DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 2 No. Task 1. Django g et started. 2. Django Architecture

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 4

Download and install "Django" latest version : C:\Python37\Scripts> pip install Django ==2.2.1

Or

C:\Python37\Scripts> pip install Django

What is Django?

Django is a high-level Python Web framework that encourages rapid development and

clean pragmatic design.

A Web framework is a set of components that provide a standard way to develop

websites fast and easily.

Django’s primary goal is to ease the creation of complex database-driven websites.

Some well known sites that use Django include PBS, Instagram, Disqus, Washington

Times, Bitbucket and Mozilla.

Features of Django

Fast: Django is ridiculous fast. It encourages rapid development with a clean and pragmatic

design. It is free and open source which helps the developers to complete their app as fast as

possible. Django takes care of much of the hassle of Web development without needing to

reinvent the wheel.

Tons of Packages: Django contains set of components that helps you to develop your

websites faster and easier. You don’t need to download it separately as Django installs all the

extras, packages and the related dependencies to handle common web development tasks. It

also takes care of user authentication, content administration, site maps and many more.

Secure: Django is highly secure as lot more work has been done there by the python web

community. It helps the developers to avoid many common security mistakes, such as SQL

injection, cross-site scripting, csrf and click jacking. Its user authentication system provides a

secure way to manage user accounts and passwords.

Versatile – Django is used to build all sort of things – from content management systems to

social networks to scientific computing platforms. Therefore, Django is extremely versatile in

all fields.

Page 5: PYTHON DJANGO - dbgyan.files.wordpress.com · DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 2 No. Task 1. Django g et started. 2. Django Architecture

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 5

Step :2

Django Architecture

Django follows MVC- MVT architecture. MVC stands for Model View Controller. It is used for developing the web publications, where we break the code into various segments. Here we have 3 segments, model view and a controller. Model – Model is used for storing and maintaining your data. It is the backend where your database is defined. Views – In Django templates, views are in html. View is all about the presentation and it is not at all aware of the backend. Whatever the user is seeing, it is referred to a view. Controller – Controller is a business logic which will interact with the model and the view. Django MVT pattern. MVT stands for Model View Template. In MVT, there is a predefined template for user interface. In the case of MVT, Django itself takes care of the controller part, it’s inbuilt.

In the above image, template is your front end which will interact with the view and the model will be used as a backend. Then view will access both the model and the templates and maps it to a url. After that, Django plays the role of controller and serves it to the user.

Page 6: PYTHON DJANGO - dbgyan.files.wordpress.com · DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 2 No. Task 1. Django g et started. 2. Django Architecture

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 6

Step : 03

Django - Creating a Project

Creating a web application, first create a project i.e. “Dbgyanweb”. To create a project, just enter into a directory where you would like to share your code, and then run the following command:

C:\ django-admin startproject dbgyanweb

Create Project Dbgyanweb directories and files. Once your project has been created, a list of files inside the project directory. each one of them are follows .

Dbgyanweb/

Manage.py

Dbgyanweb/

__inti__py

setting.py

urls.py

wsgi.py

manage.py – It is a command-line utility that lets you interact with this Django project in various ways. Dbgyanweb/ – It is the actual Python package for your project. It is used to import anything, say – Dbgyanweb.urls. init.py – Init just tells the python that this is to be treated like a python package. settings.py – This file manages all the settings of your project. urls.py – This is the main controller which maps it to your website. wsgi.py – It serves as an entry point for WSGI compatible web servers.

Page 7: PYTHON DJANGO - dbgyan.files.wordpress.com · DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 2 No. Task 1. Django g et started. 2. Django Architecture

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 7

Step : 04

Create an Application

Create your application, make sure you are in the same directory as manage.py and then type the below command:

C:\ cd dbgyanweb

C:\dbgyanweb\python manage.py startapp webapp

import your application manually inside

your project settings. For that, open your

Dbgyanweb/settings.py and add “webapp”

manually:

Created myapp application and like project, Django create a “webapp” folder with the application structure −

webapp/

__init__.py

admin.py

models.py

tests.py

views.py

__init__.py − Just to make sure python handles this folder as a package. admin.py − This file helps you make the app modifiable in the admin interface. models.py − This is where all the application models are stored. tests.py − This is where your unit tests . views.py − This is where your application views .

Add webapp

Page 8: PYTHON DJANGO - dbgyan.files.wordpress.com · DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 2 No. Task 1. Django g et started. 2. Django Architecture

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 8

Step : 05

Creating a views

Create a view: Open your webapp/views.py and put the below code in it:

Code : from django.shortcuts import render from django.http import HttpResponse def index(request): return HttpResponse("<H2> Welcome to www.dbgyan.wordpress.com ! </H2>")

Screen shot :

A view function, or “view” for short, is simply a Python function that takes a web request and

returns a web response. This response can be the HTML contents of a Web page, or a redirect, or a

404 error, or an XML document, or an image, etc.

Example: You use view to create web pages, note that you need to associate a view to a URL to see

it as a web page.

Page 9: PYTHON DJANGO - dbgyan.files.wordpress.com · DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 2 No. Task 1. Django g et started. 2. Django Architecture

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 9

Step : 06

URL mapping

Code:

from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.index, name='index'), ]

Code:

from django.contrib import admin from django.urls import include, path urlpatterns = [ path('webapp/', include('webapp.urls')), path('admin/', admin.site.urls), ]

We have created a view which returns httpResponse. Now we need to map this view to a URL. We need a URLconf in our application. Create a new python file “urls.py” inside our webapp. In webapp/urls.py include the following code:

In the above code, we have referenced a view which will return index (defined in views.py file). The url pattern is in regular expression format where ^ stands for beginning of the string and $ stands for the end. The next step is to point the root URLconf at the webapp.urls module. Open your Dbgyanweb/urls.py file and write the below code:

The marked line maps the URL "/webapp" to the webapp view created in webapp/view.py file. As you can see above a mapping is composed of three elements − The pattern − A regexp matching the URL you want to be resolved and map. Everything that can work with the python 're' module is eligible for the pattern (useful when you want to pass parameters via url). The python path to the view − same as when you are importing a module. The name − In order to perform URL reversing, you’ll need to use named URL patterns as done in the examples above. Once done, just start the server to access your view via :http://127.0.0.1/webapp

Page 10: PYTHON DJANGO - dbgyan.files.wordpress.com · DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 2 No. Task 1. Django g et started. 2. Django Architecture

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON DJANGO 1.0 Page 10

Step : 07

Start the server

C:\dbgyanweb\python manage.py runserver

Start the server and see what happens. To start the server, type the below command:

After running the server, go to http://localhost:8000/webapp/ into the browser, and you should see the text “ Welcome to www.dbgyan.wordpress.com ! ”, which you defined in the index view.

We have successfully created a basic Web App