大聖畫廊v0.2(2015.7.17),v0.22015.7.17

來源:互聯網
上載者:User

大聖畫廊v0.2(2015.7.17),v0.22015.7.17
0.2版本添加的功能

  • 以tag分類圖片
  • 美化,增加瀑布流效果
  • 增加tag頁和單張圖片頁
  • 增加發布圖片頁

以下是詳解。

每增加一個功能,都要從模型,模板,視圖,路由四個方面一一修改。

模型:增加tag屬性

思考了一下按照昨天想的分類方法還是沒辦法考慮完全,於是決定用tag標籤分類法。
首先在models.py中建立一個Tag類,只有一個name屬性

class Tag(models.Model):    name=models.CharField(max_length=20,null=False,unique=True,default="")    def __unicode__(self):        return self.nameadmin.site.register(Tag)

然後在Photo類中指定多對多屬性tags

tags=models.ManyToManyField(Tag,related_name='has_photos',blank=True)
模板:增加tag頁,photo頁和post頁

分別用來展示一個tag包含的所有圖片和單個圖片。所以tag的展示應該和首頁是差不多的、

tag.html
    {% extends "base.html" %}    {% block title %}{{tag.name}}{% endblock %}    {% block content %}    <div class="jumbotron">      <h1 align="center">{{tag.name}}</h1>    </div>    <div class="grid-sizer"></div>    {% for photo in tag.has_photos.all %}    <div class="grid-item">      <a href="p/{{photo.id}}/">    <img src="{{photo.url}}">      </a>      <p>{{photo.title}}</p>      <p>{% for tag in photo.tags.all %} <a href='/t/{{tag.id}}/'>{{tag.name}}</a> {% endfor %}</p>    </div>    {% endfor %}    {% endblock %}

在這裡在上面設定了一個巨幕來展示tag的名稱,下面放片。
tag變數由視圖傳入,然後通過tag.has_photos.all擷取該tag所有的圖片。

photo.html
    {% extends "base.html" %}    {% block title %}{{photo.title}}{% endblock %}    {% block content %}    <div class="jumbotron" align="center">      <h3>{{photo.title}}</h3>      <img src="{{photo.url}}" >    </div>    <div class="grid-sizer"></div>    {% endblock %}

傳入photo然後展示,很簡單。

post.html

post頁面需要一個表單來發布圖片資訊

    {% extends "base.html" %}    {% block title %}發布新圖片{% endblock %}    {% block content %}    <form class="form-horizontal panel container" method="POST" action=".">{% csrf_token %}        <div class="form-group">            <label  class="control-label" for="exampleReply">標題(必填,每張圖片都必須有名字哦):</label>            <input type='text' name='title' value="" class="form-control" id="exampleReply" placeholder=""></input>        </div>        <div class="form-group">            <label  class="control-label" for="exampleReply">連結(不支援直接上傳,可以先把圖片發布到堆糖等網站再把連結粘貼過來,謝謝合作):</label>            <input type='text' name='url' value="" class="form-control" id="exampleReply" placeholder=""></input>        </div>        <div class="form-group">            <label  class="control-label" for="exampleReply">標籤(多個可用空格隔開):</label>            <input type='text' name='tags' value="" class="form-control" id="exampleReply" placeholder=""></input>            <label  class="control-label" for="exampleReply">最好使用含義明確的名詞或形容詞,能夠指明圖片來源和圖片類型最好。已有的便簽見下</label>        </div>        <div class="form-group col-md-2">            <input type="hidden" name="next" value="/"/>            <input type="submit" class="btn btn-lg btn-primary" value="發布"/>        </div>    </form>    <ul class="list-group">    {% for tag in tags %}    <li class="list-group-item"><span class="badge">{{tag.}}</span>{{tag.name}}</li>    {% endfor %}    </ul>    {% endblock %}

post傳遞三個參數,圖片title,圖片連結url,還有tag,tag以字串形式傳遞,在視圖檔案中進行分割並和圖片綁定。

路由:增加tag,photo和post的連結

加了三行而已:

    url(r'^post/$',post,name='post_page'),    url(r'^t/(?P<id>\d+)/$',show_by_tag,name='tag_page'),    url(r'^p/(?P<id>\d+)/$',show_photo,name='show_photo_page'),

以id而不是名字的方式在連結中傳遞資訊,其實是偷個懶,直接根據id擷取對象要方便很多。

視圖:增加三個視圖post視圖

稍微有點複雜,用於展示表單,也用於接收處理資料

    def post(request):        if request.method=='POST':            title=request.POST.get('title')            url=request.POST.get('url')                tags=request.POST.get('tags').split()            new_photo=Photo.objects.create(                title=title,                url=url            )            if tags:                for tag in tags:                    new_tag,dummy=Tag.objects.get_or_create(name=tag)                    new_photo.tags.add(new_tag)#add tag to new_photo                    new_photo.save()            return HttpResponseRedirect('/')        else:            tags=Tag.objects.all()            return render_to_response('post.html',RequestContext(request,{'tags':tags}))

當傳輸方法是POST時,擷取post過來的資料,據此建立一個photo對象,然後把標籤字串切割一個分成單個的標籤,加到photo的tags屬性裡面去,然後儲存,跳轉到首頁。

當傳輸方法是GET時,直接展示表單頁面,不過這裡我在表單後面展示了所有存在的tag。

tag和photo視圖
    def show_by_tag(request,id):        tag=Tag.objects.get(pk=id)        return render_to_response('tag.html',RequestContext(request,{'tag':tag}))    def show_photo(request,id):        photo=Photo.objects.get(pk=id)        return render_to_response('photo.html',RequestContext(request,{'photo':photo}))

擷取連結中id對應的tag和photo,然後傳遞參數就可以了

依然是用pythonanywhere發布的:
大聖畫廊

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.