Django(一)

1、新建工程 & 新建应用

django-admin.py startproject zqxt_tmpl
cd zqxt_tmpl
python manage.py startapp learn

2、把应用learn添加到setting.py中:

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

3、打开 learn/views.py 写一个首页的视图

from django.shortcuts import render

def home(request):
    return render(request, 'home.html')

4、在 learn目录下新建一个 templates 文件夹,里面新建一个 home.html

├── learn
│   ├── admin.py
│   ├── admin.pyc
│   ├── apps.py
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── templates
│   │   ├── home.html
│   │   └── home.html-backup
│   ├── tests.py
│   ├── views.py
│   └── views.pyc

5、在 home.html 中写内容

<!DOCTYPE html>
<html>
<head>
    <title>欢迎光临</title>
</head>
<body>
欢迎光临CHEN HANG
</body>
</html>

6、将视图函数对应到网址,更改 chen_tmpl/urls.py

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

from learn import views as learn_views

urlpatterns = [
    url(r'^$', learn_views.home, name='home'),

    url(r'^admin/', admin.site.urls),
]

7、运行

python manage.py runserver

8、浏览器

127.0.0.1:8000

 

 

 

 

 

 

 

dd

posted @ 2017-04-06 19:43  航飞冲天  阅读(219)  评论(0编辑  收藏  举报