web programming session 12: django web...
TRANSCRIPT
CE419 Web Programming Session 12: Django Web Framework
Django Web Framework
• Django is a prominent member of a new generation of Web frameworks.
Django
• Django is named after Django Reinhardt, a gypsy jazz guitarist from the 1930s to early 1950s. To this day, he’s considered one of the best guitarists of all time.
• Django is pronounced JANG-oh. Rhymes with FANG-oh. The “D” is silent.
History
• A bunch of dudes in Lawrence, Kansas, USA.
• The story goes like this:
1. Write a Web application from scratch.
2. Write another Web application from scratch.
3. Realize the application from step 1 shares much in common with the application from step 2.
4. Refactor the code so that application 1 shares code with application 2.
5. Repeat steps 2-4 several times.
6. Realize you’ve invented a framework.
Model-Template-View
• Django is an MTV framework!
• "Django appears to be a MVC framework, but you call the Controller the view, and the View the template. How come you don’t use the standard names?"
• We will talk about it soon.
1. Design Your Models
• Although you can use Django without a database, it comes with an object-relational mapper in which you describe your database layout in Python code.
from django.db import models
class User(models.Model): username = models.CharField(max_length=100) password = models.CharField(max_length=100) class NumberRequest(models.Model): user = models.ForeignKey(User) start = models.IntegerField() end = models.IntegerField() generated_number = models.IntegerField()
models.py
2. Design Your URLs
• A table of contents for your app, it contains a simple mapping between URL patterns and Python callback functions.
from django.conf.urls import patterns
urlpatterns = patterns('', (r'^random/$', 'views.random_form'), (r'^generate/$', 'views.generate'), (r'^contact-us/$', 'views.contact_us'), )
urls.py
3. Write Your Views
• Each view is responsible for doing one of two things:
• Returning an HttpResponse object containing the content for the requested page.
• Raising an exception such as Http404.
• Generally, a view retrieves data according to the parameters, loads a template and renders the template with the retrieved data.
3. Write Your Views (cont'd)
from .models import NumberRequest
def generate(request): start = int(request.POST['start']) end = int(request.POST['end']) result = random.randint(start, end)
RandomRequest.objects.create(start=start, end=end, generated_number=number, user=request.user)
return render(request, "result.html", { 'result': result })
views.py
4. Design Your Templates
• Django’s template language is designed to strike a balance between power and ease. It’s designed to feel comfortable to those used to working with HTML.
<!doctype html> <html> <head> <title>Random Number Generator</title> </head> <body> <h1>Result</h1> <p>The result is {{ result }}</p> </body> </html>
result.html
Django is an MTV framework. Why MTV?
Installing Django
• Install latest Django version (1.7.6) from https://www.djangoproject.com/download/
• You can test if you installed Django successfully:
~ $ python Python 3.4.2 (v3.4.2:c0e311e010fc, May 18 2014, 00:54:21) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin (clang-500.2.78)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import django >>> django.get_version() '1.7.4'
Start a Project
• A project is a collection of settings for an instance of Django, including database configuration, Django-specific options and application-specific settings.
~$ django-admin startproject [projectname]
Django Default Project Structure
myblog/ ├── manage.py └── myblog ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py
Django Project Structure: manage.py
• A command-line utility that letsyou interact with this projectin various ways.
• Creating new apps inside project,running development web server,creating database tables from models and a lot of other tasks are done through this.
myblog/ ├── manage.py └── myblog ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py
~/myblog $ python manage.py [command]
Django Project Structure: myblog/myblog/
• The inner myblog/ directory is the actual Python package for your project.
myblog/ ├── manage.py └── myblog ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py
Django Project Structure: settings.py
• Settings/configuration for thisDjango project.
• Let's take a look at it!
• Impromptu showtime!
myblog/ ├── manage.py └── myblog ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py
Django Project Structure: urls.py
• The URLs for this Django project.
• Think of this as the “table of contents” of your site.
myblog/ ├── manage.py └── myblog ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py
http://instagram.com/accounts/login/ http://instagram.com/accounts/logout/ http://instagram.com/accounts/edit/ http://instagram.com/p/0CWuetKphk/ http://instagram.com/sadjad90 http://instagram.com/kamyarinfinity
Django Project Structure: wsgi.py
• An entry-point for WSGI-compatible web servers toserve your project.
myblog/ ├── manage.py └── myblog ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py
Django Project Structure: Apps
• Typically every Django project consists of apps.
• The term application describes a Python package that provides some set of features. Applications may be reused in various projects.
~/myblog $ python manage.py startapp posts
Django Project Structure: Apps (cont'd)
• We'll talk about all this in the future :)
posts ├── __init__.py ├── admin.py ├── migrations │ └── __init__.py ├── models.py ├── tests.py └── views.py
Django Development Server
• The Django development server (also called the runserver after the command that launches it) is a built-in, lightweight Web server you can use while developing your site.
• It’s included with Django so you can develop your site rapidly, without having to deal with configuring your production server (e.g., Apache) until you’re ready for production.
~/myblog $ python manage.py runserver
Django Development Server
• NEVER use it for production!
What good is a course without showtimes?!
Showtime!
References
• http://djangoproject.com
• http://djangobook.com
�26
Any questions?
Thank you.
�27