web programming session 12: django web...

27
CE419 Web Programming Session 12: Django Web Framework

Upload: others

Post on 23-Aug-2021

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

CE419 Web Programming Session 12: Django Web Framework

Page 2: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

Django Web Framework

• Django is a prominent member of a new generation of Web frameworks.

Page 3: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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.

Page 4: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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.

Page 5: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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.

Page 6: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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

Page 7: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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

Page 8: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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.

Page 9: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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

Page 10: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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

Page 11: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

Django is an MTV framework. Why MTV?

Page 12: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy
Page 13: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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'

Page 14: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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]

Page 15: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

Django Default Project Structure

myblog/ ├── manage.py └── myblog ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py

Page 16: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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]

Page 17: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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

Page 18: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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

Page 19: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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

Page 20: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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

Page 21: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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

Page 22: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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

Page 23: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

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

Page 24: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

Django Development Server

• NEVER use it for production!

Page 25: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

What good is a course without showtimes?!

Showtime!

Page 26: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

References

• http://djangoproject.com

• http://djangobook.com

�26

Page 27: Web Programming Session 12: Django Web Frameworkce.sharif.edu/courses/94-95/2/ce419-1/resources/root/... · 2020. 9. 7. · Django • Django is named after Django Reinhardt, a gypsy

Any questions?

Thank you.

�27