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

Lesson 13 - Finishing up Lists and Detail Views

Lesson 13 - Finishing up Lists and Detail Views

Dana Spiegel

December 05, 2012
Tweet

More Decks by Dana Spiegel

Other Decks in Technology

Transcript

  1. Redirecting the Root Path • Use a redirect at the

    root path to get people to the right default page • Don’t want to have 2 URLs for a single resource • Django provides a simple redirect view, like direct_to_template, called redirect_to • Takes a url in view dictionary • Automatically redirects browser to redirect url every time url is requested 2 from django.views.generic.simple import redirect_to url(r'^$', redirect_to, { 'url': 'team', }, name='softball_home'),
  2. Adding Links to Other Pages • url template tag makes

    computing urls for other site pages easy • Using page name as specified in urls.py, generate URL for specific pages • Include parameters for URLs that require them • Use url tag in href attributes • Always use url tag instead of hardcoding URL so it is easy to change location of resource without worrying about every template and view that links to it 3 url(r'^team/$', 'softball.views.team_list', name='team_list'), url(r'^team/(?P<team_id>\d+)/$', 'softball.views.team_view', name='team_view'), <a href="{% url team_view team_id=game.away_roster.team.pk %}"> {{ game.away_roster.team.name }}</a>
  3. Adding Bootstrap Base Template • Clone a local copy of

    repo to local machine • Download Bootstrap: http://twitter.github.com/bootstrap/ • Impement base.html • Copy static assets to appropriate directory • Add assets to git • Push them to Github, then pull them to dev server • Implement navigation bar with links to Teams, Players, Games • Make tables look pretty • Implement page scaffolding in base.html and in team and player templates • Add icons for home team and to signify win 4 <tr {% if game.winner == team %}class="text-success"{% else %} class="text-error"{% endif %}> ... ! <td>{{ game.away_score }} to {{ game.home_score }} {% if game.winner == team %}<i class="icon-ok"></i>{% endif %}</td> </tr>
  4. Rendering Nice Pages • default filter renders specified default text

    if value being rendered evaluates to False • 0, None, False, ", {}, [], set([]) • Use default_if_none if only None should be substituted with default string • Integrate icons inline with text to indicate different states • Bootstrap provides lots of icons that can be integrated inline with text • Use the <i> element with a specific class that indicates icon to be displayed • Change text or row colors based on different conditions • Add a class to the element and use coloring in CSS 5 <td class="right">{{ player.batting_average|default:"-" }}</td> {% if game.winner == team %}<i class="icon-ok"></i>{% endif %} <tr {% if game.winner == team %}class="text-success" {% else %}class="text-error"{% endif %}>
  5. Adding Error Pages • Every project should create 3 error

    pages that are automatically served by Django: • 403 - Forbidden • 404 - Page Not Found • 500 - Server Error • 403/404 are served when the resource at the view can’t be found or is not allowed to be presented to the user (anonymous or otherwise) requesting it • Regular templates which can extend and access context variables • Extend your base template, but be sure to include useful information to the user • 500 is special - Django serves this page whenever it handles an otherwise unhandled exception • Template received NO context variables, which means it should be as self contained as possible 6
  6. Work in Class • Finish Lists: • Teams, Players, Games

    • Finish Detail Views: • Team (with Players and Games) • Players (with Games) • Games (with Rosters) 7