首页 > 代码库 > Django常用命令及配置(Django 1.8.6)
Django常用命令及配置(Django 1.8.6)
常用命令
#新建Django项目django-admin startproject mysite(项目名)#新建一个APPcd mysitepython manager.py startapp app01(app名)#同步数据库python manage.py makemigrationspython 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服务器ip18 ‘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/‘ #静态文件访问url29 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超时时间,单位是秒
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 ]
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 ]
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对象,如果不是,比如数字,则返回一个TypeError10 def __unicode__(self): 11 return self.server_categ_name12 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_name20 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_name30 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
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)
Django常用命令及配置(Django 1.8.6)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。