Django中各文件常用参数详解

1.Django常用命令

#新建Django项目
django-admin startproject mysite(项目名)

#新建一个APP
cd my-site
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 #可以看到详细的列表

2.settings.py文件

AUTH_USER_MODEL = 'users.UserProfile'#设置覆盖默认的User模型,其值引用一个自定义的模型。
INSTALLED_APPS = ( #添加新建的app名到下面
    ... ...
    'app01',
)

TEMPLATES = [
{
    ... ...
    'DIRS': [os.path.join(BASE_DIR, 'templates')], #添加模板文件路径,多个路径用逗号分隔
    ... ...
]


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', #使用MySQL引擎
        'HOST':'127.0.0.1', #MySQL服务器ip
        'PORT':3306, #MySQL端口号
        'NAME':'djangotest', #MySQL数据库名
        'USER': 'root', #MySQL用户名
        'PASSWORD':'123456', ##MySQL用户密码
    }
}


#STATIC_ROOT = os.path.join(BASE_DIR,'static') #定义静态文件根目录

STATIC_URL = '/static/' #静态文件访问url
STATICFILES_DIRS = [ #定义静态文件路径,多个路径用逗号隔开。---注意,该配置不与STATIC_ROOT同时使用
    os.path.join(BASE_DIR,'static')
]


#session超时设置
SESSION_EXPIRE_AT_BROWSER_CLOSE = True #在浏览器关闭时session超时

SESSION_COOKIE_AGE = 60*30 #设置session超时时间,单位是秒        

mysite/mysite/settings.py


3.项目的urls.py文件

#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from django.conf.urls import include, url
from django.contrib import admin


urlpatterns = [
    url(r'^admin/', include(admin.site.urls)), #默认添加的后台管理功能
    url(r'^app01/', include('app01.urls')), #将所有访问app01的utl都重定向到app01的urls.py文件
]

mysite/mysite/urls.py


4.APP的urls.py文件

#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from django.conf.urls import include, url


urlpatterns = [
    url(r'^login/', 'app01.views.login'), #url为login,调用app01的views.py文件下的login函数来实现
    url(r'^index/', 'app01.views.index'),
    url(r'^user/', 'app01.views.user'),
    url(r'^usergroup/', 'app01.views.usergroup'),
    url(r'^host/', 'app01.views.host'),
]

mysite/app01/urls.py


5.APP的models.py 文件
from django.db import models

# Create your models here.
class server_fun_categ(models.Model): #创建名为server_fun_categ的表
    id = models.IntegerField(primary_key=True, db_column='ID') #定义表字段id:整型、主键、列名为ID
    server_categ_name = models.CharField(max_length=20) #定义表字段server_categ_name:char类型、最大长度为20
    
    #Django特有方法,输出对象(models.table.object.all()获取的是一个对象)的某个值
    #注意:该方法返回一个unicode对象,如果不是,比如数字,则返回一个TypeError
    def __unicode__(self): 
        return self.server_categ_name


class server_app_categ(models.Model):
    id = models.IntegerField(primary_key=True, db_column='ID')
    server_categ_id = models.ForeignKey(server_fun_categ)
    app_categ_name = models.CharField(max_length=30)
    def __unicode__(self):
        return self.app_categ_name


class server_list(models.Model):
    server_name = models.CharField(max_length=13)
    server_wip = models.GenericIPAddressField()
    server_lip = models.GenericIPAddressField(unique=True)
    server_op = models.CharField(max_length=10)
    server_app_id = models.ForeignKey(server_app_categ)
    def __unicode__(self):
        return self.server_name

class module_list(models.Model):
    id = models.IntegerField(primary_key=True, db_column='ID')
    module_name = models.CharField(max_length=20)
    module_caption = models.CharField(max_length=255)
    module_extend = models.CharField(max_length=2000)
    def __unicode__(self):
        return self.module_name

mysite/app01/models.py

6.APP的admin.py文件 .py文件 )

#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from django.contrib import admin
from autoadmin import models

# Register your models here.

class ServerlistAdmin(admin.ModelAdmin):
    list_display = ('server_name','server_wip','server_lip','server_op','server_app_id') #admin中列表显示项
    search_fields = ('server_name','server_wip','server_lip','server_op','server_app_id') #添加搜索功能
    list_filter = ('server_name','server_wip','server_lip','server_op','server_app_id') #添加快速过滤功能

class ServerappAdmin(admin.ModelAdmin):
    list_display = ('server_categ_id','app_categ_name')
    search_fields = ('server_categ_id','app_categ_name')
    list_filter = ('server_categ_id','app_categ_name')

class ServerfunAdmin(admin.ModelAdmin):
    list_display = ('server_categ_name',)
    search_fields = ('server_categ_name',)
    list_filter = ('server_categ_name',)

class ModulelistAdmin(admin.ModelAdmin):
    list_display = ('module_name','module_caption','module_extend')
    search_fields = ('module_name','module_caption','module_extend')
    list_filter = ('module_name','module_caption','module_extend')

 

admin.site.register(models.server_list,ServerlistAdmin) #将模块添加到admin后台,进行可视化管理
admin.site.register(models.server_app_categ,ServerappAdmin)
admin.site.register(models.server_fun_categ,ServerfunAdmin)
admin.site.register(models.module_list,ModulelistAdmin)

mysite/app01/admin.py




你可能感兴趣的:(Django)