python - Creating An Archive List For A Blog -


I am trying to create a collection list with the count using SQLAlchemy, Flask and Zilla which looks like this . I do not want to show those months who do not have the post. I do not understand how to calculate the unique year, months and calculations for each dictionary. (2) - December (2) - December 2014 (13) - January (3) - February (8) - March (2)

Model

  Class Post (DB Modell): '' 'Returns the return table' '' Id = db.Column (db.Integer, primary_key = true) title = db.Column (db.String (80)) url = db.Column (db.String ( 120), Unique = True) body = db. Column (db. Text) create_date = db.Column (pdf), pub_date = db.Column (db.DateTime) pub_status = db.Column (db.Text (80)) author_id = db.Column (db.Integer, Db .forwardkeyey ('user.id') = Db.backref ('post', lazy = 'dynamic')) topic = db.relationship ('subject', backref = db.backref ('post', lazy = 'dynamic')) # Many-to-many tags = Db.relationship ('tag', secondary = post_tag, backf = 'post', lazy = 'dynamic')  

UPDATE

This code builds the dictionary, but not sure how I will add the calculations. any idea? Is there a better way?

View

  # Custom Filter converts the number of months to "January", etc. @App.template_filter ('Month_number') def month_nname (month_number): return calendar.month_name [month_number] @ app.route ('/ archive') def display_archive (): p = db.session.query (Post.pub_date). Removes duplicate months by entering all () d = defaultdict (set) # set. N = i} p: d [i.pub_date.year] .add (i.pub_date.month) d.items () # The first part completes a return set and completes two months, requires sorting. # Defaultdict (& lt; type 'set' & gt ;, 2012: set ([1, 2, 4]), \ # 2013: set ([4, 12, 5, 6]), 2014: set ([1 , 2, 3])}} For the key, value in d.iteritems (): a = value a = list (a) #convert list sets in A.sort () #Soft list N [key] = returns Render_tomplate ('archive.html', d = d, n = n) Convert to list for easy sorting. # {2012: [1, 2, 4], 2013: [4, 5, 6, 12], 2014: [1, 2, 3]}  

jinja2 template

& lt; Dl & gt; {% Key, n.iteritems ()%} & lt; Dt & gt; {{Key}} & lt; / Dt & gt; {% V value in}% lt; Dd & gt; & Lt; A href = "/ {{key}} / {{v}}" & gt; {{V. Month_number}} & lt; / A & gt; & Lt; / Dd> {% Endfor%} {% endfor%} & lt; / Dl & gt; Again, it works ... but not sure how to add calculations. I think it must be done before the defaultdict function.

Solution is basically a solution posted in response.

  @ app.route ('/ archive') def display_archive (): p = db.session.query (Post.pub_date) .all () d = defaultdict (list) for i In P: d [i.pub_date.year] .append (i.pub_date.month) # Add monthly calculations by counting the number of examples. The value in adict.iteritems (), adds posttotal = 0 monthly and yearly joints to the key, in d.items (), adict [k] = counter (v) bdict = {} # in k for adict = { }: For years = 0, value.items () in value: year = +++ post target = c bdict [key] = year d = defaultdict (list) for k, adict.items () in v Bdict.items (): D [k] .append (v) return render_template ('archive.html', d = d)  

jina2

  {% Extension of "base .html"%} {% block title%} Archive {% endblock%} {% block content%} & lt; Dl & gt; {% Key, in d.iteritems ()%} & lt; Dt & gt; & Lt; A href = "/ {{key}}" gt; {{Key}} & lt; / A & gt; ({{Value [1]}}) & lt; / Dt & gt; For {% M, value in [0] .it ()%} < Dd & gt; & Lt; A href = "/ {{key}} / {{m}}" & gt; {{M | Month_number}} & lt; / A & gt; ({{C}}) & lt; / A & gt; & Lt; / Dd> {{A}} {% endfor%} {% endfor%} & lt; / Dl & gt; {% Endblock%}  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -