RapidSMS Developers Guide/Web Views & Templates

From Wikibooks, open books for an open world
Jump to navigation Jump to search

In web views, usually we want to represent some feature of our app to be viewed in a web page dynamically such as statistics, reports or even data manipulation.

Views[edit | edit source]

In your app create a new file and name it views.py , in our case inside app/survey

A view receives an http request and returns http response We import the class HttpResponse, which lives in the django.http module

from django.http import HttpResponse

Inside views we define the functions that will take http request as parameter , the view returns an HttpResponse object that contains the generated response. Each view function is responsible for returning an HttpResponse object. so we define our index function as the following :

from django.http import HttpResponse
from models import Survey

def index (request):    
    result = Survey.objects.all()
       
    return  render_to_response(request,'survey/index.html',{'allrecords':result})

This function simply retrieves data objects from the models (dataBase) and pass them to template to be displayed in a well format view.

    render_to_response(template[, dictionary][, context_instance][, mimetype])

Renders a given template with a given context dictionary and returns an HttpResponse object with that rendered text.

For more details please visite :django views

URLS mapping file[edit | edit source]

In your app create new file urls.py which will be a simple mapping between urls patterns and a call back function (our views) In this file import urls defaults and the views from our views that we have just created:

from django.conf.urls.defaults import *
import views

we should configure urls file to customize our views : We have tow views in our app/ survey : Main view views.index and views.profile

urlpatterns = patterns('',
    url(r'^survey/?$', views.index),
    url(r'^survey/(\d+)/?$', views.profile, name='profile')
)

Patterns is available from django config urls defaults , we passe the first parameter as empty and we passe other parameters as needed

The 'r' in front of each regular expression string is optional but recommended. It tells Python that a string is "raw"

\d appeared in the group regular expression is a parameter that we passe to profile view

Templates[edit | edit source]

Template is a text file that can generates any text-based format as (html,xml,..) Template can contains a variables which you will replace their values in your code variable looks like this: Template:Allrecords.Template compiler will evaluates that variable and replaces it with the result.

We have to create a template were we will write html code to view the data as reports or statistics or any thing that we want to display in our web application.

Create a new folder in your app and name it template.

apps/survey/template

Inside this folder create your template .html files.

index.html, profile.html

Template example :

{% extends base_template %}
{% block content %}
<head>
</head>
<body>
<h1> Survey result </h3>
<h2>Summery </h2>
<table border="1">
<tr> <td> Number of records </td> <td> {{allrecords.count()}}</td></tr>
<tr> <td> First name</td> <td> {{allrecords.firstName}}</td></tr>

</table>
</body>
{% endblock %}

Template inheritance[edit | edit source]

To reuse Django's template is template inheritance. Template inheritance allows you to build a base "skeleton" template that contains all the common elements of your site and defines blocks that child templates can override. we put our code inside blocks to inherit from django main template as below.

{% extends base_template %}
{% block content %}

……

{% endblock %}

For more information about templates please visit: [templates language]


Customizing Admin U.I