This is a creation in Article, where the information may have evolved or changed.
First, the time format in the database
1 classarticle (models. Model):2 """3 Article Table4 """5AID = models. Bigautofield (primary_key=True)6title = models. Charfield (verbose_name="article title", max_length=128)7Summary = models. Charfield (verbose_name="about the article", max_length=255)8Read_count = models. Integerfield (default=0)9Comment_count = models. Integerfield (default=0)TenUp_count = models. Integerfield (default=0) OneDown_count = models. Integerfield (default=0) ACreate_time = models. Datetimefield (verbose_name="creation Time", auto_now_add=True) - -Blog = models. ForeignKey (verbose_name="Affiliated Blog", to="Blog", to_field="Bid") theCategory = Models. ForeignKey (verbose_name="article Type", to="Category", to_field="CID", Null=true, Related_name="Articles") - -Type_choice = [ -(1,"Python"), +(2,"Linux"), -(3,"OpenStack"), +(4,"Golang"), A ] at -ARTICLE_TYPE_ID = models. Integerfield (Choices=type_choice, default=None) - -tags =models. Manytomanyfield ( -to="Tag", -Through="Article2tag", inThrough_fields= ("article","Tag"), -Related_name="Articles", to) models.py
Processing in Sqlite3
defHome (Request, site):"""bo Master personal home:p Aram Request::p Aram site: Blogger's website suffix such as: Http://xxx.com/wupeiqi.html:return:""" #generate three fields nid,num,ctime dates are grouped by%y-%m monthDate_list = models. Article.objects.raw (#raw Execute native SQL statements 'Select Nid, Count (nid) as Num,strftime ("%y-%m", Create_time) as CTime from Repository_article Group by strftime ("%y-%m ", Create_time)') #Date_format (Create_time, "%y-%m") # Top is sqllist way, MySQL change strftime to Date_format can #Date_list method of extracting data forIteminchdate_list:Print(item.nid,item.num,item.ctime)#4 1 2017-01 #8 2017-02 #7 1 2017-03
Processing in MySQL
defFilter (Request, site, condition, val):---snip---elifCondition = ='Date': #how MySQL is executed #article_list = models. Article.objects.filter (Blog=blog). Extra ( #where=[' Date_format (create_time, "%%y-%%m") =%s '], Params=[val,]). All () #extra constructs additional query conditions or mappings, such as: Sub-queryArticle_list = models. Article.objects.filter (Blog=blog). Extra (#Val passed in date parameters such as: 2017-02where=['strftime ("%%y-%%m", Create_time) =%s'], Params=[val,]). All ()#Python two% indicates a% character var corresponds to%s #The above statement is equivalent to the SELECT * from article where strftime ("%y-%m", Create_time) =2017-02
Second, the time format in the template
fromDjangoImportTemplate fromDjango.utils.safestringImportMark_safeImportTimeregister=template. Library () @register. Simple_tagdefconversion_time (date, format):ifFormat = ="%y-%m-%d%h:%m": Date= str (DATE) [:-14] Date= Time.strptime (date,"%y-%m-%d%h:%m:%s") Date= Time.mktime (date) + 28800Date= Time.gmtime (date + 28800) Date=time.strftime (format, date)returnMark_safe (date)elifFormat = ="%y-%m-%d": Date= str (DATE) [:-14] Date= Time.strptime (date,"%y-%m-%d%h:%m:%s") Date= Time.mktime (date) + 28800Date= Time.gmtime (date + 28800) Date=time.strftime (format, date)returnMark_safe (date)elifFormat = ="%y-%m": Date= str (DATE) [:-14] Date= Time.strptime (date,"%y-%m-%d%h:%m:%s") Date= Time.mktime (date) + 28800Date= Time.gmtime (date + 28800) Date=time.strftime (format, date)returnMark_safe (date)