Share
Embed
Transcript
31 slides
-
Anatomy of a Web Application Server Side Client Side Web
Server Database Web Browser -
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 -
What we want to do today name=CMU “Hello CMU!” sayHello
service http://localhost/HelloYou/ http://localhost/HelloYou/sayHello/ Hello CMU! -
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 -
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 -
Creating a Django project • The project is the repository
for all of your webapps ➡ Create a project $ django-admin.py startproject projectname -
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) -
Testing the server • Start the server • Open the
page in your browser $ python manage.py runserver -
Creating a Django app ➡ Create an app $ python
manage.py startapp helloyou The app name It must be different than the project name -
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 -
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 -
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 -
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 -
What a webapp contains • Each app has its own:
• URLs • HTML documents (called templates in Django) • static files (CSS, JS and media files) -
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 -
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’))] -
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 -
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 -
What we want to do - Hello You! yourname=CMU “Hello
CMU!” say_hello service http://localhost/HelloYou/ http://localhost/HelloYou/sayHello/ Hello CMU! -
Passing arguments • Different ways to exchange arguments between the
client and the server • REST style (Representational state transfer) • HTTP/GET • HTTP/POST • Cookie -
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) -
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) -
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) -
pros and cons Good Bad REST resources passwords and files
GET resources passwords and files POST password and files resources