django 项目的基本配置

cera cera

django 项目的基本配置

建立项目

django-admin startproject django4

建立APP

    cd django4
    python manage.py startapp app01

配置模板文件路径

settings.py

INSTALLED_APPS 添加一个APP

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app01'
]

配置静态文件存放位置

新建一个目录为static

STATICFILES_DIRS=(
				    os.path.join(BASE_DIR,'static'),
				)

配置模板文件的存放位置    建立目录templates

'DIRS': [os.path.join(BASE_DIR),'templates'],
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR),'templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

URL 分发

在主目录里面的url.py 中

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


urlpatterns = [
    url(r'admin/', admin.site.urls),
    url(r'^app/', include("app01.url")),
]

然后在app01文件夹中建立一个url.py

例如:这个是我的一个配置

from app01 import views
from django.conf.urls import url



urlpatterns = [
    url(r'^login/', views.LogIn),
    url(r'^orm/',views.orm),
    url(r'^index/',views.Index),
    url(r'^user_info/',views.user_info),
    url(r'^user_group/',views.user_group),
    url(r'^userdetail-(?P<nid>\d+)/',views.userdetail),
    url(r'^userdel-(?P<nid>\d+)/',views.userdel),
    url(r'^useredit-(?P<nid>\d+)/',views.useredit),
]

cera cloudiplc tengxunyun

相关推荐