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

Lesson 10 - Model-based Views

Lesson 10 - Model-based Views

Dana Spiegel

December 05, 2012
Tweet

More Decks by Dana Spiegel

Other Decks in Technology

Transcript

  1. Softball Tracker Views • Admin provides an easy way to

    manage and view data in web app • Meant for admin users and internal staff • How does everyone else get access to view the information in the database and update their data? • Django views allow creation of any type of display of data, in any format • Provide complete control of how information is presented and can prevent users from interacting with other users’ data • Creating views driven by data and models is similar to creating non-data driven views (as we just reviewed) 2
  2. Adding a View: URL Mapper • Configure urls.py in application

    • Dependent urls.py is specific to the application and only deals with the specific application’s views • Primary urls.py imports application urls.py • Application urls.py is given a path for users to use 3 from django.conf.urls import patterns, include, url from django.views.generic.simple import direct_to_template urlpatterns = patterns('softball.views', url(r'^$', 'team_list', name='team_list'), ) urlpatterns = patterns('project.views', ... url(r'^softball/', include('softball.urls')), )
  3. Adding a View: Build Template • Building template before writing

    view helps to illustrate data required from view to make information presentation work • Clearly define and code: • Any objects that will be needed to present information on page • Structure and format of any data passed to template • Methods for transforming data objects into any complex presentation, like tables • Templates usually live in the primary templates directory • Create a directory with the name of the application • Mock up HTML template to ensure the data will look reasonable • Ensure images and CSS required by template are placed into application directory within static directory • Create a test view that just renders template to validate HTML presentation • Create a new base template if appropriate 4
  4. Adding a View: Build Template • First view: Team list

    page • Template should expect a collection of teams and render their names and # of players in a table • Create a base.html template in templates/softball • Add blocks for the title and the body of the page • Create the team list.html template in templates/softball/team • Fill in the title and body blocks • Need data from view method: • List of team objects as a QuerySet 5 {% extends 'softball/base.html' %} {% block title %}Teams{% endblock %} {% block body %} {% for team in teams %} <p>{{ team.name }}</p> {% endfor %} {% endblock %}
  5. Adding a View: The View Method • View method does

    the actual work of collecting the information that will be passed into template • From template, need a QuerySet of all teams • Should provide an ordering so list is in a known order • Put all data into context dictionary and pass into TemplateResponse method • Django lazy-processes the query 6 from django.template.response import TemplateResponse import models def team_list(request): """ Lists all teams in the Database """ teams = models.Team.objects.all().order_by('name') return TemplateResponse(request, 'softball/teams/list.html', { 'teams': teams, })
  6. In Class: Adding a Player List • Add a view

    that shows all Players in a table • Display name and other Player attributes • Order by Player name • Add template templates/softball/player/list.html • Add view to urls.py 7