Upgrade to Pro — share decks privately, control downloads, hide ads and more …

15-437 Django

ThierrySans
September 10, 2013

15-437 Django

ThierrySans

September 10, 2013
Tweet

More Decks by ThierrySans

Other Decks in Education

Transcript

  1. Server Side Programming ➡ Idea: Having a program that generates

    webpages ✓ To process data from users (i.e from the browser) ✓ To access server resources • Database, filesystem, other programs and so on ✓ To provide different resources in different contexts • Authentication, sessions
  2. What we want to do today name=CMU “Hello CMU!” sayHello

    service http://localhost/HelloYou/ http://localhost/HelloYou/sayHello/ Hello CMU!
  3. What I will teach you today • How to write

    your first Django app: Hello World • How to better organize your Django project (not in tutorials) • How to process arguments: Hello You
  4. How we are going to develop webapps • Django (1.9)

    is a Python framework to develop webapps ✓ Django embeds a web server for development purpose ๏ But it is not a (real) web server that will be used for production
  5. Creating a Django project • The project is the repository

    for all of your webapps
 ➡ Create a project $ django-admin.py startproject projectname
  6. Structure of a Django project tsans/ manage.py tsans/ __init__.py settings.py

    urls.py wsgi.py A command-line utility that lets you interact with this Django project in various ways. Settings/configuration for this Django project. The URL declarations for this Django project; a "table of contents" of your Django-powered site. A configuration file to deploy your Django application (later in this course)
  7. Testing the server • Start the server
 • Open the

    page in your browser $ python manage.py runserver
  8. Creating a Django app ➡ Create an app $ python

    manage.py startapp helloyou The app name It must be different than the project name
  9. Adding the App in your Project # Application definition INSTALLED_APPS

    = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'helloyou' ) tsans/settings.py
  10. Structure of a Django app helloyou/ migrations __init__.py admin.py apps.py

    models.py test.py views.py Contains data definition (that will be stored in the database) Contains unit tests Contains the services
  11. Writing your first service • Defining the service
 • Associating

    the views with URLs from django.http import HttpResponse def index(request): return HttpResponse("Hello world!") helloyou/views.py from helloyou import views urlpatterns += [ url(r'^helloyou/', views.index, name='index'), ] tsans/urls.py
  12. Returning an HTML document def index(request): page = '''\ <html>\

    <head>\ <title>Hello You</title>\ </head>\ <body>\ Hello World!\ <body>\ </html>\ ''' return HttpResponse(page) helloyou/views.py
  13. What a webapp contains • Each app has its own:

    • URLs • HTML documents (called templates in Django) • static files (CSS, JS and media files)
  14. A better structure for your Django app helloyou/ __init__.py models.py

    test.py views.py urls.py migrations/ templates/ helloyou/ static/ helloyou/ /css /js /media Contains app-specific urls Contains app-specific 
 HTML templates Contains app-specific 
 static files
  15. Decoupling project URLs and app specific URLs • Django project

    URL • App specific URLs from . import views
 urlpatterns +=[url(r'^$', views.index, name=‘index’)] helloyou/urls.py tsans/urls.py from django.conf.urls import include, url
 urlpatterns += [url(r'^helloyou/', include(‘helloyou’))]
  16. Defining an HTML template {% load staticfiles %} <html> <head>

    <title>Hello You</title> <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> <script src="{% static "helloyou/js/script.js" %}"></script> </head> <body> <form action='sayhellopost/' method='post'> <input type="text" name="yourname"/> </form> <select id="passing-method"> <option value="REST">REST</option> <option value="GET">GET</option> <option value="POST">POST</option> </select> <button type="button" id="submit">Hello You!</button> <body> </html> helloyou/templates/helloyou/index.html
  17. Working with an HTML template • Call a template as

    the HTTP response helloyou/views.py from django.shortcuts import render def index(request): return render(request,'helloyou/index.html',{}); We will see more advance techniques to work with templates later
  18. What we want to do - Hello You! yourname=CMU “Hello

    CMU!” say_hello service http://localhost/HelloYou/ http://localhost/HelloYou/sayHello/ Hello CMU!
  19. Passing arguments • Different ways to exchange arguments between the

    client and the server • REST style (Representational state transfer) • HTTP/GET • HTTP/POST • Cookie
  20. REST style • Define the view (that takes an argument

    yourname) • Extract the parameter from the URL urlpatterns += [ url(r'^$', views.index, name='index'), url(r'^sayhellorest/(?P<yourname>\w+)/$', views.sayHelloRest, name='sayHelloRest') ] helloyou/urls.py helloyou/views.py def sayHelloRest(request, yourname): return HttpResponse("Hello %s!" % yourname)
  21. HTTP/GET • Define the view that extract the argument from

    the request • Define the URL urlpatterns += [ url(r'^$', views.index, name='index'), url(r'^sayhelloget/$', 
 views.sayHelloGet, name=‘sayHelloGet’) ] helloyou/urls.py helloyou/views.py def sayHelloGet(request): yourname = request.GET['yourname'] return HttpResponse("Hello %s!" % yourname)
  22. HTTP/POST • Define the view that extract the argument from

    the request • URLs are defined in the same way as for GET helloyou/views.py from django.views.decorators.csrf import csrf_exempt @csrf_exempt def sayHelloPost(request): yourname = request.POST['yourname'] return HttpResponse("Hello %s!" % yourname)
  23. pros and cons Good Bad REST resources passwords and files

    GET resources passwords and files POST password and files resources