17
 Web Frameworks Purposes of Web Frameworks Facilitate writing web applications (handles the low level work) Designing urls Handling requests Receiving form submissions Handling cookies and sessions Sending responses Storing data Frameworks are constantly worked on and updated by people all over the world to help facilitate web development

Web Frameworks - University of North Texas Libraries Frameworks Purposes of Web Frameworks Facilitate writing web applications (handles the low level work) Designing urls Handling

  • Upload
    buihanh

  • View
    221

  • Download
    0

Embed Size (px)

Citation preview

   

Web Frameworks Purposes of Web Frameworks Facilitate writing web applications (handles the low level work)

Designing urls

Handling requests

Receiving form submissions

Handling cookies and sessions

Sending responses

Storing data

Frameworks are constantly worked on and updated by people all over the world to help facilitate web development

   

Web Frameworks Full Stack Web Frameworks Most parts of the framework are built into framework, or implemented by the 

framework designers

Django, Ruby on Rails, CakePHP

Built from Component Web Frameworks Most components of the framework are interchangeable parts, usually 

developed by 3rd parties, left up to the programmer to integrate

CherryPy – Web Application Component

Cheetah – Templating Component

Combination Web Frameworks Have parts that are built in, and parts that are interchangable

TurboGears – Has CherryPy, Kid, SQLObject and MochiKit built in

   

Web Frameworks MVC (Ruby on Rails and CakePHP) Model – View – Controller

Model – Database object, interface to database, field validation

View – Renders the output displayed to the user (HTML)

Controller – Handles request, peforms operations on Model data, sends data to the View

MTV (Django) Model – Template – View

Model – Database object, interface to database, field validation

Template – Describes how output should be rendered to the user

View – Handles request, performs operations on Model data, renders the Template to produce the output displayed to the user

   

Web Frameworks

   

Web Frameworks

   

Web Frameworks Cake Request

   

Web Frameworks A Specific Look at Django A typical Django web application consist of four basic things:

models.py

urls.py

views.py

templates

   

Web Frameworks Example Modelclass Book(models.Model):    title = models.CharField(max_length=255)    author = models.CharField(max_length=100)    publisher = models.CharField(max_length=100)    description = models.TextField()    publish_date = models.DateField(auto_now=True)    collection = models.ForeignKey(Collection)

class Collection(models.Model):    author = models.CharField(max_length=100)    name = models.CharField(max_length=255)

   

Web Frameworks Example ORM (Object­relational mapping) API

Creating/Editing a record:

book_entry = Book(title='Django',                               author='Adrian Holovaty',                               publisher='OReiley',                               description='Great book about Django',                               collection=collection_entry,                              )book_entry.save()

   

Web Frameworks Example ORM (Object­relational mapping) API

Retrieving a Record:

#Remove Booksbook_list = Book.objects.filter(title__icontains='Harry Potter')for book in book_list:

book.delete()

#Find Books in a Collectioncard_collection = Collection.objects.get(author='Orson Scott Card')card_books = Book.objects.filter(collection=card_collection)

   

Web Frameworks Dynamic URLs

You design your URLs and map them to functionsurlpatterns = patterns('',    url(r"collections^$", collection_listing, name='collection_listing'),    http://www.example.com/collections/

    url(r"^about/$", about, name='about'),    http://www.example.com/about/

    url(r"([^/]+)/$", view_collection, name='view_collection'),    http://www.example.com/OrsonScottCard/

    url(r"([^/]+)/add/$", book_add, name='book_add'),    http://www.example.com/OrsonScottCard/add/

    url(r"([^/]+)/about/$", collection_about, name='collection_about'),    http://www.example.com/OrsonScottCard/about/

    url(r"([^/]+)/search.json$", search_collection_json, name='search_collection_json'),    http://www.example.com/OrsonScottCard/search.json)

   

Web Frameworks Creating a View

def view_collection(request, collection_name):    collection_list = Collection.objects.all()

    return render_to_response(        'collection.html', #The template you are rendering        {         #The variables you need in your template         'collection_list': collection_list,        },        RequestContext(request, {}),        )

   

Web Frameworks Creating a View

def view_collection(request, collection_name):    collection_list = Collection.objects.all()

    return render_to_response(        'collection.html', #The template you are rendering        {         #The variables you need in your template         'collection_list': collection_list,        },        RequestContext(request, {}),        )

   

Web Frameworks Base Template (base_template.html)

<html>

<head><title>{% block title %}{% endblock %}</title>

<link rel="stylesheet" href="styles/local.css" type="text/css" /><script type="text/javascript" src="scripts/local.js"></script>

{% block extra_header %}{% endblock %}</head>

<body>{% block content %}{% endblock %}{% block footer %}{% endblock %}</body>

</html>

   

Web Frameworks Extending Templates

Collection List Template ”collection.html”:{% extends "base_template.html" %}

{% block title %}Collection List{% endblock %}

{% block extra_header %}<meta name="robots" content="noindex" /><script type="text/javascript">    //extra javascript</script>{% endblock %}

Continued On Next Page...

   

Web Frameworks Extending Templates (continued)

{% block content %}    <div id="hd"><h1>Collection List</h1></div>    {% if user.is_authenticated %}    <div>        <ul>        {% for collection in collection_list %}            <li>{{ collection.name|capfirst }}</li>        {% endfor %}        </ul>    </div>    {% else %}    <div>You do not have permission to view this collection</div>    {% endif %}

{% endblock %}

{% block footer %}<div>This is a University of North Texas Site</div>{% endblock %}

   

Web Frameworks

Things to check out: Django pre­written Apps

Django's built­in Admin

Websites: www.djangoproject.com tutorial

www.cakephp.org

www.rubyonrails.org

Freenode.net IRC Channels: #django, #rubyonrails, #cakephp

Google App Engine ­ Django