首页 > 代码库 > Django(一)

Django(一)

1、新建工程 & 新建应用

django-admin.py startproject zqxt_tmplcd zqxt_tmplpython 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 renderdef 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 urlfrom django.contrib import adminfrom learn import views as learn_viewsurlpatterns = [    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

Django(一)