Django 基础开发

创建工程

django-admin startproject holiday

打开服务

》》》python manage.py runserver
Performing system checks…
System check identified no issues (0 silenced)
You have unapplied migrations; your app may not work properly until they are applied.
Run ‘python manage.py migrate’ to apply them.
July 15, 2017 - 10:07:03
Django version 1.8.17, using settings ‘holiday.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Django 基础开发_第1张图片
工程 tree
Django 基础开发_第2张图片

在holiday的holiday目录下编写;
view.py 文件代码:

from django.http import HttpResponse

def home(request):
    return HttpResponse("i like you ,this is a begin")

urls.py 文件代码:

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

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', view.home)
]

demo:
Django 基础开发_第3张图片

在第一个holiday目录下创建一个templates文件夹,在templates下创建一个HTML

修改setting.py文件,添加

‘DIRS’: [BASE_DIR+”/templates”,],

即:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR+"/templates",],   #change
        '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',
            ],
        },
    },
]

修改view.py

from django.http import HttpResponse
from django.shortcuts import render
def home(request):
    return render(request,'home.html',)

刷新即可见;

数据库 操作
修改setting.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',  # 或者 mysql.connector.django
        'NAME': 'test',
        'USER': 'test',
        'PASSWORD': 'tes',
        'HOST':'localhost',
        'PORT':'3306',
    }
}

创建APP(爱胖胖)
尽量不要使用中文!

django-admin startapp app_first
现在的目录tree:
Django 基础开发_第4张图片

修改app_first下的models.py文件代码;

from django.db import models

# Create your models here.
class Test(models.Model):
    name = models.CharField(max_length=20)

修改setting.py文件代码;

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'TestModel', #add
)

创建表结构
命令行;

$ python manage.py migrate   # 创建表结构

$ python manage.py makemigrations TestModel  # 让 Django 知道我们在我们的模型有一些变更
$ python manage.py migrate TestModel   # 创建表结构
F:\Python(test_document)\Django_web\holiday>python manage.py makemigrations app_first
Migrations for 'app_first':
  0001_initial.py:
    - Create model Test

F:\Python(test_document)\Django_web\holiday>python manage.py migrate app_first
Operations to perform:
  Apply all migrations: app_first
Running migrations:
  Rendering model states... DONE
  Applying app_first.0001_initial... OK

F:\Python(test_document)\Django_web\holiday>

我们可以看到数据库中创建了一个新表:
app_first_test
Django 基础开发_第5张图片
表名组成结构为:应用名_类名(如:app_first_test)。
表的主键会自动设为id这个字段!

demo ::::::::::::::
Django 基础开发_第6张图片

你可能感兴趣的:(python,django,python)