django- build BBS Summary of key points

0826 self-summary

django- build BBS Summary of key points

A. About chap, direct input url to access the file contents

djangoIt comes opened a hole is static files can be accessed directly

Manual chap

urs.py

from django.views.static import serve


urlpatterns = [
    url(r'^avatar/(?P<path>.*)', serve, kwargs={'document_root': 开口文件的路径}),
]
#这里的r'^avatar/(?P<path>.*),前面的路径等同于后面设置的路径,而下面正则匹配的内容为内容的拼接前面的就是完整的一个路径,这样就可以url直接访问文件夹

Issues relating to the sign-in codes in regard to solving concurrent

解决方法: The existence of the session code

注意点: If you open both the same page in a browser, the Web site after his code prevail, which is characteristic of the session

III. Verification code generated

https://www.cnblogs.com/pythonywy/p/11408318.html

SIGNIFICANT operation after login

auth module may be utilized in the login

When login successful

The objects uesr conveniently stored in a subsequent operation request

auth.login(request,user)

You can also be done to determine whether the page to sign in

{% if request.user.is_authenticated %}

Complete logout

auth.logout(request)

V. Form Find

#查询当前站点下所有标签对应的文章数

#查询当前站点下所有分类对应的文章数
# 查询所有分类对应的文章数
# 分组查询固定规则:
# filter 在annotate前表示where条件
# values 在annotate前表示group by
# filter 在annotate后表示having条件
# values 在annotate后表示取值
# category_ret=models.Category.objects.all().values('pk').annotate(cou=Count('article__nid')).values('title','cou')
# 查询当前站点下所有分类对应的文章数
category_ret=models.Category.objects.all().filter(blog=blog).annotate(cou=Count('article__nid')).values_list('title','cou','nid')
print(category_ret)
# 查询当前站点下所有标签对应的文章数
tag_ret=models.Tag.objects.all().filter(blog=blog).annotate(cou=Count('article__nid')).values_list('title','cou','nid')
print(tag_ret)
#查询某年某月下对应的文章数

'''
            from django.db.models.functions import TruncMonth
            Sales.objects
            .annotate(month=TruncMonth('timestamp'))  # Truncate to month and add to select list
            .values('month')  # Group By month
            .annotate(c=Count('id'))  # Select the count of the grouping
            .values('month', 'c')  # (might be redundant, haven't tested) select month and count

    '''
year_ret=models.Article.objects.all().annotate(month=TruncMonth('create_time')).values('month').annotate(c=Count('nid')).values_list('month','c')

关键点:

  • Key can be abbreviated to directlypk
  • About annotate
    • As long as two model classes by ForeignKey or ManyToMany associate, then you can use annotate methods to count the number.
    • annotate (Field Name = party function)
    • There are values ​​when the front annotate, mainly to accelerate the speed of query, values ​​must be in accordance annotate

VI. Custom folder storage path

settings.py

#加这两句,以后再上传的图片,都放在media文件夹下
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
# MEDIA_ROOT = os.path.join(BASE_DIR, "app01")

Guess you like

Origin www.cnblogs.com/pythonywy/p/11414750.html