Day 62 Django后台管理/MEDIA配置

Django后台管理

  1. 先去应用下的admin.py中注册你想要管理的模型类

    from django.contrib import admin
    from app01 import models
    # Register your models here.
    
    
    admin.site.register(models.Userinfo)
    admin.site.register(models.Blog)
    admin.site.register(models.Category)
    admin.site.register(models.Tag)
    admin.site.register(models.Article)
    admin.site.register(models.UpAndDown)
    admin.site.register(models.Article2Tag)
    
  2. 注册一个超级用户

    python manage.py.createsuperuser
    
  3. 登录admin页面进行数据录入

MEDIA配置

网站所用到的静态文件统一默认都放在static文件夹下

用户上传的静态文件也应该单独建立一个文件夹进行存储,无论用户上传什么类型的文件,只要是静态的,都应该放在某一固定的文件下

MEDIA配置

在settings.py中配置

MEDIA_ROOT = os.path.join(BASE_DIR,'media')

在urls.py中添加url

url(r'^media/(?P<path>.*)',serve,{'document_root':settings.MEDIA_ROOT})

TruncMonth

# django 官网给你提供了一个方法
	from django.db.models.functions import TruncMonth

# -官方提供
	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
原文地址:https://www.cnblogs.com/2222bai/p/12018743.html