0% found this document useful (0 votes)
390 views20 pages

Presentation On: Django A Python Framework For Web Applications

This presentation provided an overview of the Django web framework for Python. Django is described as a framework for "perfectionists with deadlines" that uses the MVC pattern. It includes an ORM, template language, authentication system, admin interface, and many reusable apps. The presentation covered key Django concepts like settings.py, models, views, templates, forms, and URLs. It also discussed authentication, generic class-based views, and common extras included with Django like caching, sessions, and messages.

Uploaded by

Risb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
390 views20 pages

Presentation On: Django A Python Framework For Web Applications

This presentation provided an overview of the Django web framework for Python. Django is described as a framework for "perfectionists with deadlines" that uses the MVC pattern. It includes an ORM, template language, authentication system, admin interface, and many reusable apps. The presentation covered key Django concepts like settings.py, models, views, templates, forms, and URLs. It also discussed authentication, generic class-based views, and common extras included with Django like caching, sessions, and messages.

Uploaded by

Risb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Presentation on

Django
A Python framework for Web
Applications

Rishabh Shankhdhar
1801410902
Why Python?
 Written in C – high performance, ability to link to C libraries
for extensions
 Interpreted script language compiled on the fly into bytecode
 Easier to read coding standards – whitespace sensitive
 Object Oriented
Introducing…Django
 “The framework for perfectionists with deadlines”
 MVC
 Flexible template language that can be used to generate
HTML, CSV, Email or any other format
 Includes ORM that supports many databases – Postgresql,
MySQL, Oracle, SQLite
 Lots of extras included – middleware, csrf protections,
sessions, caching, authentication
Django Concepts/Best Practices
 DRY Principle – “Don’t Repeat Yourself”
 Fat models, thin views
 Keep logic in templates to a minimum
 Use small, reusable “apps” (app = python module with
models, views, templates, test)
settings.py
 Defines settings used by a Django application
 Referenced by wsgi.py to bootstrap the project loading
 Techniques for managing dev vs prod settings:
 Create settings-dev.py and settings-prod.py and use symlink to
link settings.py to the correct settings
 Factor out common settings into base-settings.py and import.
Use conditionals to load correct settings based on DEBUG or
other setting
Sample Settings…
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = (

'django.contrib.admin',

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.messages',

'django.contrib.staticfiles',

)
Django Apps
 Reusable modules
 django-admin.py startapp <app_name>
 Creates stub layout:
<APP_ROOT>
admin.py
models.py
templates (directory)
tests.py
views.py
urls.py
Django Models
 Defined in models.py
 Typically inherit from django.db.models.Model
Example Model:

from django.db import models

class TestModel(models.Model):

name = models.CharField(max_length = 20)

age = models.IntegerField()
Selecting Objects
 Models include a default manager called objects
 Manager methods allow selecting all or some instances
Question.objects.all()
Question.objects.get(pk = 1)
Use try block, throws DoesNotExist exception if no
match
Question.objects.filter(created_date__lt = ‘2014-01-01’)
 Returns QuerySet
Quick CRUD Operations with
Generic Views
 ListView
 UpdateView
 CreateView
 If Model is specified, automagically creates a matching
ModelForm
 Form will save the Model if data passes validation
 Override form_valid() method to provide custom logic (i.e
sending email or setting additional fields)
Sample – As Class Based View
from .models import Question
from django.views.generic import ListView

class QuestionList(ListView):
model = Question
context_object_name = ‘questions’
Django Templates
 Very simple syntax:
variables = {{variable_name}}

template tags = {%tag%}


 Flexible – can be used to render html, text, csv, email, you
name it!
 Dot notation – template engine attempts to resolve by
looking for matching attributes, hashes and methods
Question List Template
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>List of Questions</title>
</head>
<body>
{%if questions%}
<ul>
{%for q in questions%}
<li>{{q.question_text}}</li>
{%endfor%}
</ul>
{%else%}
<p>No questions have been defined</p>
{%endif%}
</body>
</html>
urls.py
 Defines routes to send urls to various views
 Can use regular expressions
 Extract parameters from a url and pass to the view as a named
parameter:

r(‘^question/(?P<question_id>\d+)/$’,’views.question_detail’)
 Extensible – urls.py can include additional url files from
apps:
r(‘^question/’,include(question.urls))
Forms in Django
 django.forms provides a class to build HTML forms and
validation. Example:

from django import forms

class EditQuestionForm(forms.Form):
question_text = forms.CharField(max_length = 200)

 Often redundant when creating forms that work on a single


model
Request & Response
 Request object encapsulate the request and provide access to a number of
attributes and methods for accessing cookies, sessions, the logged in user
object, meta data (i.e environment variables),
 Response objects are returned to the browser. Can set content type,
content length, response does not have to return HTML or a rendered
template
 Special response types allow for common functionality:
HttpResponeRedirect
Http404
HttpStreamingResponse
Django Extras
 CRSF Middleware – enabled by default. Include template tag in all
forms:
{%csrf_token%}
 Authentication
 Caching
 Sessions
 Messages
 Email
 Logging
Authentication
 Django’s out of the box Auth system uses database
authentication.
 Changed extensively in Django 1.6 to allow custom User
objects.
 AUTHENTICATION_BACKENDS setting in settings.py
allows overriding how User objects are authenticated
 If using the Authentication middleware and
context_processors the current user is available to code as
request.user and {{user}} is defined in all templates
Resources
 Python – http://www.python.org
 Django – http://www.djangoproject.com
 Python Packages – https://pypi.python.org
 Django Packages – https://www.djangopackages.com
Thank You

You might also like