django项目配置流程

  •  安装MySQL-python  (python3安装的是PxMysql (和python2是不一样的)):
  • DATABASES: 
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': "online",
        'USER': "root",
        'PASSWORD': "123456",
        'HOST': "127.0.0.1",
        'POST': '3306'
    }
}
  • TEMPLATES下的DIRS:  在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',
            ],
        },
    },
]
  • 新建STATICFILES_DIRS
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)
  • 生成数据表

生成迁移文件

manage.py makemigrations

备注: 在migrations目录中生成一个迁移文件,此时数据库中还没生成表单

执行迁移

 manage.py migrate

备注: 相当于执行了MySQL语句创建了表单

  • 视图基本使用

在Django中,视图对web请求进行回应;视图其实就是一个python函数,在views.py文件中定义。 有几个页面就有几个视图。

定义视图:(其中my.html是templates中的html)

def index(request):
    return render(request,'my.html')
  • 配置urls
from firstapp.views import index        #导入视图
urlpatterns = [
    url(r'^admin/', admin.site.urls),

    url(r'^index/$', index)     # ^表示以index为开头,$表示以/结尾的地址
]

 运行,在浏览器中输入127.0.0.1:8000/index/ ,成功!


参考:https://blog.csdn.net/gulang03/article/details/79328868

猜你喜欢

转载自blog.csdn.net/Floating__dream/article/details/86543537