DJANGO - Pythonclassroomdiary.files.wordpress

3y ago
21 Views
2 Downloads
2.55 MB
41 Pages
Last View : 16d ago
Last Download : 3m ago
Upload by : Giovanna Wyche
Transcription

DJANGOBy : Sangeeta M Chauhan , Gwaliorwww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Django is a free and open source webapplication framework which offers fast andeffective dynamic website development.What is framework?A web framework is a code library thatmakes web development faster andeasier by providing common patternsfor building reliable, scalable andmaintainable web applicationswww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

To understand it more clear lets understand thedifference between library and framework Python LibraryFrameworkProgram Codewww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Characteristics of Django Loosely Coupled Django helps you to make eachelement of its stack independent of the others. Less code - Ensures effective development Not repeated- Everything should be developed inprecisely one place instead of repeating it again Fast development- Django's offers fast and reliableapplication development. Consistent design - Django maintains a cleandesign and makes it easy to follow the best webdevelopment practices.www.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Some of the popular sites that uses Django are : Pinterest (tool for collecting and organizing yourfavorite things) , Instagram , Knight Foundation, Mozilla, National Geographic Open Knowledge Foundation Disqus (most popular discussion system) Chess etcwww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Django MVT (Model –View-Template)The MVT is a software design pattern which includesthree important components Model, View and Template. The Model helps to handle database. It is a data accesslayer which handles the data. The Template is a presentation layer which handlesUser Interface part completely. The View is used to execute the business logic andinteract with a model to carry data and renders atemplate.Although Django follows MVC pattern but maintains it’sown conventions. Here control is handled by theframework itself.www.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Flow of Control in MODEL-VIEW-TEMPLATESends requests for aresorceDjangoMODELUSERChecksavailabilityof resourceURL in URLVIEWView interacts with Model &Template. Then renders atemplateIf url is mappedView is calledTEMPLATEAt last Django responds backto the user and sends atemplate as a response.www.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Now our very first step is to createVirtual Envioronment The virtual environment is an environmentwhich is used by Django to execute anapplication. It is recommended to create andexecute a Django application in a ress.com Sangeeta M Chauhan, Gwalior

Virtual Environment Its recommended to install Virtualenv before installingDjango . Virtual Environment creates isolated environments toisolates your Python files on a per-project basis. It ensure that any changes created to your website won’t have an effect on alternative websitesyou’re developing. Theattention-grabbinghalfisthatyoujust will produce virtual environments with completelydifferent python versions, with every environmenthaving its own set of packages.www.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

pip3.5VirtualEnvioronment2.5Virtual Env 1Virtual Env 1Django 1.6Django 1.43.6Virtual Env 1Django 1.0www.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

What should be Used .Virtual Enviornment or wrapper? virtualenv is a tool to create isolated Pythonenvironments. The virtualenv creates a folder whichcontains all the necessary executables to use thepackages that a Python project would need. virtualenvwrapper is a set of extensionsto virtualenv tool. The extensions include wrappers forcreating and deleting virtual environments andotherwise managing our development workflow,making it easier to work on more than one project at atime without introducing conflicts in ss.com Sangeeta M Chauhan, Gwalior

Using virtualenv without virtualenvwrapper is alittle bit painful because every time we want toactivate a virtual environment, so we have totype long command like this: myproject/env/activate But with virtualenvwrapper, we only need totype: workon myprojectwww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

With windows, to installvirtualenviornmentwrapper . type pip install press.com Sangeeta M Chauhan, Gwalior

Steps to install virtual environmentand DjangoStep 1 : Go to Window Power Shell (Admin)/command windowStep 2: Move to desired drive ( C:, D:, F: etc.) bytyping driveName : (example F: ) and move tofolder by typing cd foldername cd DjangoProj inthis caseStep 3: typeF:\ pip install virtualenvwrapper –win(to install virtaulenvwrapper in F: drive)www.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Steps to install virtual environment and DjangoStep 4 : Create virtual environment under the folderDjangoProj by typing mkvirtualenv projenvStep 5 : now type workon projenvStep 6 : Now install Django by typing pip install djangoStep 7 : Now we will create Django Project namely LIBRARYF:\ DjangoProj\MyProj django- admin startproject Librarywww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

A folder Library will be created underf:\DjangoProj it will look like this :www.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Now to check whether the DjangoServer is running or not :F:\DjangoProj\Library python manage.py runserverThis address will betyped in the browserto check whether theDjango server isworking properlywww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

If you see thisscreen, your Djangoserver is ess.com Sangeeta M Chauhan, Gwalior

Now you will notice that following Django enviornment will becreated Inside the main Libraryfolder 1 subfolder with samenameand1file‘manage.py’will be createdInside the subfolder LIbrary 4 files willinit .pysettings.pyurls.pywsgi.pywill be createdwww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

How these files interact each otherwww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Django Project Structure manage.py: a shortcut to use the django-admin commandline utility. It’s used to run management commands related toour project. We will use it to run the development server, runtests, create migrations and much more. init .py: It is an empty file that indicates that this folder isa Python package. settings.py: It contains all the project’s configuration. urls.py: With the help of this file we can map the routes andpaths in our project. For example, if you want to showsomething in the URL /about/, you have to map it here first. wsgi.py: this file is a simple gateway interface used fordeployment. We have nothing to do with it.www.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

After creating Project lets create Django App Firstly make sure your Django server is not running ,if it is so stop it by pressing ctrl C Now again switch to Command window or powershell Now move to the folder Proj1 (main folder )App Name Here typecan be givenas per your django-admin startapp bookchoicewww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Django App “book” created nowwww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

In “book” App following files will becreated m Sangeeta M Chauhan, Gwalior

Lets understand working of these files migrations/: This folder stores some files to keep trackof the changes done in models.py file, so to keep thedatabase and the models.py updated. admin.py: It is a configuration file for a built-in Djangoapp called Django Admin. apps.py: It is a configuration file of the current app. models.pyThis is the file where we define the entities ofour Web application. The models are translatedautomatically by Django into database tables. tests.py: used to write unit tests for the app. views.py: this is the file where we handle therequest/response for our Web application.www.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Now that we created our first app book under theProject Library, let’s configure our project to use it.open the settings.py with IDLEandsearchforthe INSTALLED APPS variable :IINSTALLED APPSVARIABLEwww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Now create any .html file which you want to show withDjango and place it under the new folder ‘template’under the folder ‘Library’Here I havecreated file“First.html”Now we will add this file to views.py so that we can view it with Django Serverwww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Now Open settings.py with IDLE to register newlycreated app book in INSTALLED APPS list and addnewly created ‘template’ folder in TEMPLATES listBy including App ‘book ‘and template folder we aretelling the project thattemplate folder includes allpages of app ‘book’ inside itwww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Now write code in views file to callhtml filerender() is aspecial Djangohelper functionthat creates ashortcut forcommunicatingwith a webbrowserwww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Setting views in url :Now open URL.py file anddo the following changes to link filesWrite importstatement toinclude viewsfrom bookHere we areincluding name ofwebpage (FIRST) tobe executed withserver and functionto be .com Sangeeta M Chauhan, Gwalior

Now we are ready to run our webpageFIRST.html with Django ServerType in the webbrowserhttp://127.0.0.1:8000/FIRSTThen this web pagewill be displayedwww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Hurray !!!!! we have developed ourfirst basic WebPage Successfully.Lets create a webpage which sends Some Dataabout books through form and we will save itin database(GET & POST ) Methodwww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Before we start lets understand the basic conceptsrequired to develop such applicationWhat is HTTP?HTTP is a set of protocols designed to enablecommunication between clients and servers. It works as arequest-response protocol between a client and server.A web browser may be the client, and an application on acomputer that hosts a web site may be the server.To request a response from the server, there are mainlytwo methods: GET : to request data from the server. POST : to submit data to be processed to the server.www.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

The post() method is used when youwant to send some data to the server. Syntax requests.post(url, data {key: value},json {key: value}, args)www.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Lets Start to .We are going to continue with previously createdproject “Library” and App “Book”Now first step is to create HTML form to input bookdetails.www.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Here we have created a html file under template folder to create3 text boxes to input book code, book name and authorname .you can add more controls to read more data as per yourneedFile Name :bookdetail.htmlDon’t forget to add1. action “#” (to tellthis form will beprocessed within file/url)2. {%csrf token%} in html fileCsrf token is usedto send requests tothe server, inwhichthe token om Sangeeta M Chauhan, Gwalior

Now create a view :open view.py under the App book and add the followingfunctionHere Bookentry function iscreated which1. store the POSTed datainto book dict dictionaryobject2. creating a csv filebooks.csv and writingthe data into itwww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Setting.py : Here we have already done the requiredchanges with previosly created webpage. So now thereis no need to do any update Url.py :Add this lineNow we are ready to run this application Open browser and typelocalhost:8000/bookdetail in address barwww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

It will show you form you have createdusing html “bookdetail.html”Note : You can add as many record as you want using thisformwww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Now you will notice a new file iscreated under your projectSee contentsare written inthis fileWe can open this file with Excel orNotepadwww.pythonclassroomdiary.wordpress.com Sangeeta M Chauhan, Gwalior

Now its your turnto create ss.com Sangeeta M Chauhan, Gwalior

Django MVT (Model –View-Template) The MVT is a software design pattern which includes three important components Model, View and Template. The Model helps to handle database.It is a data access

Related Documents:

Session 18 (Working with Django Web Framework) Installing Django, Setting Up Django Working with Web Application Development Django API, Django Admin App, Django Views Django Templates CRUD Operation using Django Working with Django with BootStrap 4.0 Working with Django Forms Django Cl

Python Django Certification Training 3 P a g e About the Program Intellipaat’s Django course makes you master the Django REST framework, Django models, Django AJAX, Django jQuery, etc. In this best Python Django course, you will learn about the web framework by doi

Django Included Apps django.contrib.auth An authentication system. django.contrib.contenttypes A framework for content types. django.contrib.sessions A session framework. django.contrib.sites A framework for managing multiple sites with one Django installation. django

Django ORM Cookbook Documentation, Release 2.0 Django ORM Cookbook is a book about doing things with Django ORM and Django models. Django is a "MTV" (Model-Template-View) framework - This book provides a deep dive into the Mpart. They take the form of about 50 questions of the form How to do X with Django ORM/Queryset/Models. Contents 1

Django Admin Cookbook is a book about doing things with Django admin. It is targeted towards intermediate Django developers, who have some experience with Django admin, but are looking to expand their knowledge of Django

Django Blog – Ch 1-3 Opening Report 02 – 09/07 – 09/13 Django Blog – Ch 1 to 3 Django Deployment – Ch14 Your Django Blog 03 – 09/14 – 09/20 SEO Strategy – Ch 4-6 Django Social Website - Ch 4-6 04 – 09/21 – 09/27 Django Social Website - Ch 4-6 Your Social Site

Capítulo 14: Django desde la línea de comandos. 60 Observaciones 60 Examples 60 Django desde la línea de comandos. 60 Capítulo 15: Django Rest Framework 61 Examples 61 Barebones simples API de solo lectura 61 Capítulo 16: Django y redes sociales 63 Parámetros 63 Examples 64 Manera fácil: python-social-auth 64 Usando Django Allauth 67

Python Django & React Js will be the best Choice. Complete full-stack web development course with Python - Django as backend and ReactJS in the frontend. Along with this, we will use a Payment Gateway and SMS Gateway in our E-Commerce Web and Other Applications. Eligibility: This Online Training Program on React Js - Django is for all students and