1. app下创建 templates
运行的时候 先找全局的templates——》 按照app的注册顺序找templates中的文件
2. app下在创建一个urls.py
include()
3. 创建超级用户
python manage.py createsuperuser
邮箱可以忽略,密码默认为8位
4. 在admin中注册model
from django.contrib import admin from crm import models admin.site.register(models.Customer) admin.site.register(models.ClassList) admin.site.register(models.Campuses)
5. 使用
6.不同的字典不同显示
a.普通字段 : {{ customer.qq }}
b.含有choices字段: {{ customer.get_sex_display }} # get_字段名_display() 方法 模板中不加()
c. 多对多、特殊显示,在model中定义方法。
def show_classes(self): return ' | '.join([str(i) for i in self.class_list.all()]) def show_status(self): color_dict = { 'signed': 'green', 'unregistered': 'red', 'studying': 'blue', 'paid_in_full': 'yellow', } return '<span style="color: white;padding: 3px">{}</span>'.format( color_dict.get(self.status), self.get_status_display())
admin.py
from django.contrib import admin from app1 import models # 在admin后台注册某个字段 admin.site.register(models.Customer) admin.site.register(models.ClassList) admin.site.register(models.Campuses) admin.site.register(models.UserProfile)
from django.contrib import admin from app1 import models # 在admin后台注册某个字段 admin.site.register(models.Customer) admin.site.register(models.ClassList) admin.site.register(models.Campuses) admin.site.register(models.UserProfile)
总的urls.py
from django.contrib import admin from django.conf.urls import url, include from app1 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^login/', views.login), url(r'^index', views.index), url(r'^reg', views.reg), url(r'app1/', include('app1.urls')) ]
setting.py 部分
USE_L10N = False #本地格式化 USE_TZ = True #格式化时间 DATETIME_FORMAT = "Y-m-d H:i:s" DATE_FORMAT = "Y-m-d"