二 Django 1.5.4 Views and Templates

一.修改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 %}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值