一.修改beer/views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from beer.models import Beer, Brewery
def BeersAll(request):
beers = Beer.objects.all().order_by('name')
context = {'beers': beers}
return render_to_response('beersall.html', context, context_instance=RequestContext(request))
def SpecificBeer(request, beerslug):
beer = Beer.objects.get(slug=beerslug)
context = {'beer': beer}
return render_to_response('singlebeer.html', context, context_instance=RequestContext(request))
def SpecificBrewery(reqeust, breweryslug):
brewery = Brewery.objects.get(slug=breweryslug)
beers = Beer.objects.filter(brewery=brewery)
context = {'beers': beers}
return render_to_response('singlebrewery.html', context, context_instance=RequestContext(reqeust))
在static目录下创建css和images目录,images放入center_bg.jpg,和bg.jpg
创建templates目录
修改templates/base.html
<html>
<head>
<link rel="stylesheet" type="text/css" href="/static/css/video1.css" />
{% block extrahead %}
{% endblock %}
</head>
<body>
<div id="pageContainer">
{% block content %}
{% endblock %}
</div>
</body>
</html>
二.修改templates/beersall.html
{% extends "base.html" %}
{% block content %}
<div id="beerslist">
{% for beer in beers %}
<p><a href="/beers/{{ beer.slug }}/"><img src="{{ beer.image1.url }}" />{{ beer }}</a></p>
{% endfor %}
</div>
{% endblock %}
三.修改templates/singlebeer.html
{% extends "base.html" %}
{% block content %}
<div id="singlebeer">
<p>Name: {{ beer }}</p>
<p>Locality: {{ beer.get_locality_display }}</p>
<p>Brewery: <a href="/brewerys/{{ beer.brewery.slug }}/">{{ beer.brewery }}</a></p>
</div>
{% endblock %}
四.修改templates/singlebrewery.html
{% extends "base.html" %}
{% block content %}
<div id="beerslist">
<h2>Beers from {{ beers.0.brewery }}</h2>
{% for beer in beers %}
<p><a href="/beers/{{ beer.slug }}/">{{ beer }}</a></p>
{% endfor %}
</div>
{% endblock %}
五.修改urls.py
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
from django.views.generic import TemplateView
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'^$', TemplateView.as_view(tempate_name="index.html"),
(r'^beers/$', 'beer.views.BeersAll'),
(r'^beers/(?P<beerslug>.*)/$', 'beer.views.SpecificBeer'),
(r'^brewerys/(?P<breweryslug>.*)/$', 'beer.views.SpecificBrewery'),
)
六.修改static/css/video1.css
body{
background-attachment: fixed;
background-color: #000000;
background-image: url(/static/images/bg.jpg)
background-position: center top;
background-repeat: no-repeat;
color:#FFFFFF;
margin: 0;
margin: 0;
}
#pageContainer {
background: url(/static/images/center_bg.jpg) repeat-y scroll center top #000000;
margin: 0 auto;
position: relative;
width: 1000px;
min-height: 800px;
}
七.修改templates/index.html
{% extends "base.html" %}
{% block content %}
<a href="/beers/">View the Beers list!</a>
{% endblock %}