咨询热线:4006-75-4006
售前:9:00-23:30 备案:9:00-18:00 技术:7*24h
1. 创建一个 zqxt_tmpl 项目,和一个 名称为 learn 的应用,并且
1 2 3 | django - admin.py startproject zqxt_tmpl cd zqxt_tmpl python manage.py startapp learn |
2. 把 learn 加入到 settings.INSTALLED_APPS中
1 2 3 4 5 6 7 8 9 10 | 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 写一个首页的视图
1 2 3 4 5 | from django.shortcuts import render def home(request): return render(request, 'home.html' ) |
4. 在 learn目录下新建一个 templates 文件夹,里面新建一个 home.html
默认配置下,Django 的模板系统会自动找到app下面的templates文件夹中的模板文件。
目录的结构是这样的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | zqxt_tmpl ├── learn │ ├── __init__.py │ ├── admin.py │ ├── migrations │ │ └── __init__.py │ ├── models.py │ ├── templates │ │ └── home.html │ ├── tests.py │ └── views.py ├── manage.py └── zqxt_tmpl ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py 4 directories, 12 files |
5. 在 home.html 中写一些内容
1 2 3 4 5 6 7 8 9 | <!DOCTYPE html> < html > < head > < title >欢迎光临</ title > </ head > < body > 欢迎光临自强学堂 </ body > </ html > |
6. 将视图函数对应到网址,更改 zqxt_tmpl/urls.py
Django 1.7.x 及以下可以这样:
1 2 3 4 5 6 7 8 9 10 | from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r '^$' , 'learn.views.home' , name = 'home' ), # new # url(r'^blog/', include('blog.urls')), url(r '^admin/' , include(admin.site.urls)), ] |
Django 1.8.x 及以上:
1 2 3 4 5 6 7 8 9 | from django.conf.urls import include, url from django.contrib import admin from learn import views as learn_views urlpatterns = [ url(r '^$' , learn_views.home, name = 'home' ), url(r '^admin/' , include(admin.site.urls)), ] |
7. [可选] 创建数据库表
1 2 3 4 | python manage.py syncdb |