tree #树形显示项目目录结构
django页面模板标签
1.注释
{# 这是一个注释 #}
2.循坏
{% for tmp in list %}
...
{% endfor %}
3.判断
{% if condition %}
...
{% elif condition2 %}
...
{% else %}
...
{% endif %}
4.页面包含
{% include "1.html" %}
创建项目
django-admin.py startproject demo
将项目最外层目录首字母改为大写,方便区分目录
mv demo Demo
cd Demo
一、简单页面访问
(1)方法
vi demo/view.py
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world ! ")
(2)配置路由
vi demo/urls.py
from django.conf.urls import url
from . import view
urlpatterns = [
url(r'^$', view.hello), #初始目录,可选
url(r'^hello$', view.hello), #注意逗号不要丢
]
(3)加入自己的服务器ip
vi demo/settings.py
ALLOWED_HOSTS = ['gpo2.sl.xx.com']
(4)启动项目,端口可改
python3 manage.py runserver 0.0.0.0:8080 #ctrl + c 停止服务
二、加入模板方式
cd demo
mkdir templates
cd templates
vi hello.html
{{hello}} #显示后台变量
cd ..
vi settings.py #加入模板路径
'DIRS': [BASE_DIR+"/demo/templates",],
vi view.py
from django.shortcuts import render
def hello(request):
context = {}
context['hello'] = 'hello world'
return render(request, 'hello.html', context)
#重定向方式 return http.HttpResponseRedirect(urls.reverse('方法的路由名'))
新建django项目步骤:
一、新建项目
django-admin.py startproject demo #新建项目
mv demo Demo #将项目最外层目录首字母改为大写,方便区分目录
cd Demo
cd demo
二、新建core模块
python3 ../manage.py startapp core #新建core模块
mkdir templates #新建页面模块目录
cd templates
mkdir core #新建core页面模块目录
cd ../.. #返回到Demo目录下
python3 manage.py migrate #初始化migrate文件,会生成一个db.sqlite3数据库文件
三、配置model文件
vi demo/core/models.py
from django.db import models
class Book(models.Model):
name = models.CharField(max_length=20)
author = models.CharField(max_length=20)
四、配置文件(ip、目录)
vi demo/core/settings.py
ALLOWED_HOSTS = ['gpo2.sl.xx.com'] #允许ip
INSTALLED_APPS 中增加一行 'demo.core', #控制层目录
TEMPLATES 的DIRS中增加 os.path.join(BASE_DIR, 'demo/templates') #页面目录
五、建表
python3 manage.py makemigrations core #生成创建数据表脚本文件
python3 manage.py migrate #创建book表
sqlite3 db.sqlite3
.tables #可以看到有一个core_book表
.schema core_book #查看表结构,id自动生成
.quit #退出
cd demo
六、路由配置
(1)vi urls.py #总路由
from django.conf import urls
from django.contrib import admin
urlpatterns = [
urls.url(r'^admin/', admin.site.urls),
urls.url(r'', urls.include('demo.core.urls')), #包含core目录下url文件
]
(2)vi core/urls.py #core模块路由
from django.conf import urls
from django.views import generic
from demo.core import views
urlpatterns = [
urls.url(r'^books/$', views.books),
urls.url(r'^create-book$', views.create_book),
urls.url(r'^delete-book/$', views.delete_book),
urls.url(r'^view-book/$', views.view_book),
urls.url(r'^update-book$', views.update_book)
]
七、后台处理文件
vi core/view.py
from django.apps import apps
from django.shortcuts import render,redirect
from django.views import generic
from demo.core import models
def books(request):
books = models.Book.objects.all()
context = { 'books': books }
return render(request, 'core/books.html', context)
def create_book(request):
if request.method == 'GET':
return render(request, 'core/create-book.html')
elif request.method == 'POST':
name = request.POST.get('name')
author = request.POST.get('author')
book = models.Book.objects.create(name = name, author = author)
return redirect('/books')
def delete_book(request):
id = request.GET.get('id')
models.Book.objects.filter(id = id).delete()
return redirect('/books')
def view_book(request):
id = request.GET.get('id')
book = models.Book.objects.get(id = id)
context = { 'book': book }
return render(request, 'core/view-book.html', context)
def update_book(request):
if request.method == 'GET':
id = request.GET.get('id')
book = models.Book.objects.get(id = id)
context = { 'book': book }
return render(request, 'core/update-book.html', context)
elif request.method == 'POST':
id = request.POST.get('id')
name = request.POST.get('name')
author = request.POST.get('author')
models.Book.objects.filter(id = id).update(name = name, author = author)
return redirect('/books')
八、页面显示部分
(1)vi templates/core/books.html
{% if books %}
{% for book in books %}
{{ book.id }} {{ book.name }} {{ book.author }}
{% endfor %}
{% endif %}
(2)vi templates/core/create-book.html
{% csrf_token %}
name:
author:
(3)vi demo/templates/core/view-book.html
id | {{book.id}} |
name | {{book.name}} |
author | {{book.author}} |
(4)vi demo/templates/core/update-book.html
{% csrf_token %}
id | {{book.id}} |
author | |
name |
九、启动项目
python3 manage.py runserver 0.0.0.0:8080
十、停止服务
ctrl + c
项目目录结构
Demo
├── manage.py
├── db.sqlite3 #sqlite3数据库文件
└── demo
├── settings.py #配置文件
├── urls.py #总路由配置文件
├── core #core模块,自己创建的
│ ├── models.py #model文件
│ ├── views.py #控制层,数据处理,页面跳转
│ ├── helpers.py #帮助类
│ └── urls.py #core模块 路由配置文件
├── templates
│ └── core
│ ├── books.html
│ ├── create-book.html
│ ├── update-book.html
│ └── view-book.html
└──static #静态文件目录
├── css
├── js
└── image