常用命令
#新建Django项目 django-admin startproject mysite(项目名) #新建一个APP cd mysite python manager.py startapp app01(app名) #同步数据库 python manage.py makemigrations python manage.py migrate #使用开发服务器 python manage.py runserver 0.0.0.0:8000 #清空数据库 python manage.py flush #创建超级管理员 python manage.py createsuperuser # 修改用户密码可以用: python manage.py changepassword username #更多命令 python manage.py #可以看到详细的列表
常用配置
【mysite/mysite/settings.py】
1 INSTALLED_APPS = ( #添加新建的app名到下面 2 ... ... 3 'app01', 4 ) 5 6 TEMPLATES = [ 7 { 8 ... ... 9 'DIRS': [os.path.join(BASE_DIR, 'templates')], #添加模板文件路径,多个路径用逗号分隔 10 ... ... 11 ] 12 13 14 DATABASES = { 15 'default': { 16 'ENGINE': 'django.db.backends.mysql', #使用MySQL引擎 17 'HOST':'127.0.0.1', #MySQL服务器ip 18 'PORT':3306, #MySQL端口号 19 'NAME':'djangotest', #MySQL数据库名 20 'USER': 'root', #MySQL用户名 21 'PASSWORD':'123456', ##MySQL用户密码 22 } 23 } 24 25 26 #STATIC_ROOT = os.path.join(BASE_DIR,'static') #定义静态文件根目录 27 28 STATIC_URL = '/static/' #静态文件访问url 29 STATICFILES_DIRS = [ #定义静态文件路径,多个路径用逗号隔开。---注意,该配置不与STATIC_ROOT同时使用 30 os.path.join(BASE_DIR,'static') 31 ] 32 33 34 #session超时设置 35 SESSION_EXPIRE_AT_BROWSER_CLOSE = True #在浏览器关闭时session超时 36 37 SESSION_COOKIE_AGE = 60*30 #设置session超时时间,单位是秒
【mysite/mysite/urls.py】
1 #!/usr/bin/env python 2 # -*- encoding: utf-8 -*- 3 4 from django.conf.urls import include, url 5 from django.contrib import admin 6 7 8 urlpatterns = [ 9 url(r'^admin/', include(admin.site.urls)), #默认添加的后台管理功能 10 url(r'^app01/', include('app01.urls')), #将所有访问app01的utl都重定向到app01的urls.py文件 11 ]
【mysite/app01/urls.py】
1 #!/usr/bin/env python 2 # -*- encoding: utf-8 -*- 3 4 from django.conf.urls import include, url 5 6 7 urlpatterns = [ 8 url(r'^login/', 'app01.views.login'), #url为login,调用app01的views.py文件下的login函数来实现 9 url(r'^index/', 'app01.views.index'), 10 url(r'^user/', 'app01.views.user'), 11 url(r'^usergroup/', 'app01.views.usergroup'), 12 url(r'^host/', 'app01.views.host'), 13 ]
【mysite/app01/models.py】
1 from django.db import models 2 3 # Create your models here. 4 class server_fun_categ(models.Model): #创建名为server_fun_categ的表 5 id = models.IntegerField(primary_key=True, db_column='ID') #定义表字段id:整型、主键、列名为ID 6 server_categ_name = models.CharField(max_length=20) #定义表字段server_categ_name:char类型、最大长度为20 7 8 #Django特有方法,输出对象(models.table.object.all()获取的是一个对象)的某个值 9 #注意:该方法返回一个unicode对象,如果不是,比如数字,则返回一个TypeError 10 def __unicode__(self): 11 return self.server_categ_name 12 13 14 class server_app_categ(models.Model): 15 id = models.IntegerField(primary_key=True, db_column='ID') 16 server_categ_id = models.ForeignKey(server_fun_categ) 17 app_categ_name = models.CharField(max_length=30) 18 def __unicode__(self): 19 return self.app_categ_name 20 21 22 class server_list(models.Model): 23 server_name = models.CharField(max_length=13) 24 server_wip = models.GenericIPAddressField() 25 server_lip = models.GenericIPAddressField(unique=True) 26 server_op = models.CharField(max_length=10) 27 server_app_id = models.ForeignKey(server_app_categ) 28 def __unicode__(self): 29 return self.server_name 30 31 class module_list(models.Model): 32 id = models.IntegerField(primary_key=True, db_column='ID') 33 module_name = models.CharField(max_length=20) 34 module_caption = models.CharField(max_length=255) 35 module_extend = models.CharField(max_length=2000) 36 def __unicode__(self): 37 return self.module_name
【mysite/app01/admin.py】
1 #!/usr/bin/env python 2 # -*- encoding: utf-8 -*- 3 4 from django.contrib import admin 5 from autoadmin import models 6 7 # Register your models here. 8 9 class ServerlistAdmin(admin.ModelAdmin): 10 list_display = ('server_name','server_wip','server_lip','server_op','server_app_id') #admin中列表显示项 11 search_fields = ('server_name','server_wip','server_lip','server_op','server_app_id') #添加搜索功能 12 list_filter = ('server_name','server_wip','server_lip','server_op','server_app_id') #添加快速过滤功能 13 14 class ServerappAdmin(admin.ModelAdmin): 15 list_display = ('server_categ_id','app_categ_name') 16 search_fields = ('server_categ_id','app_categ_name') 17 list_filter = ('server_categ_id','app_categ_name') 18 19 class ServerfunAdmin(admin.ModelAdmin): 20 list_display = ('server_categ_name',) 21 search_fields = ('server_categ_name',) 22 list_filter = ('server_categ_name',) 23 24 class ModulelistAdmin(admin.ModelAdmin): 25 list_display = ('module_name','module_caption','module_extend') 26 search_fields = ('module_name','module_caption','module_extend') 27 list_filter = ('module_name','module_caption','module_extend') 28 29 30 31 admin.site.register(models.server_list,ServerlistAdmin) #将模块添加到admin后台,进行可视化管理 32 admin.site.register(models.server_app_categ,ServerappAdmin) 33 admin.site.register(models.server_fun_categ,ServerfunAdmin) 34 admin.site.register(models.module_list,ModulelistAdmin)
关注我的公众号,不定期推送资讯
本文来自博客园,作者:链条君,转载请注明原文链接:https://www.cnblogs.com/MacoLee/p/5841488.html