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、簡單頁面訪問
(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 中止服務
2、加入模板方式
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項目步驟:
1、新建項目
django-admin.py startproject demo #新建項目
mv demo Demo #將項目最外層目錄首字母改成大寫,方便區分目錄
cd Demo
cd demo
2、新建core模塊
python3 ../manage.py startapp core #新建core模塊
mkdir templates #新建頁面模塊目錄
cd templates
mkdir core #新建core頁面模塊目錄
cd ../.. #返回到Demo目錄下
python3 manage.py migrate #初始化migrate文件,會生成一個db.sqlite3數據庫文件
3、配置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)
4、配置文件(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') #頁面目錄
5、建表
python3 manage.py makemigrations core #生成建立數據表腳本文件
python3 manage.py migrate #建立book表
sqlite3 db.sqlite3
.tables #能夠看到有一個core_book表
.schema core_book #查看錶結構,id自動生成
.quit #退出
cd demo
6、路由配置
(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)
]
7、後臺處理文件
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')
8、頁面顯示部分
(1)vi templates/core/books.html
{% if books %}
{% for book in books %}
{{ book.id }} {{ book.name }} {{ book.author }}
<a href="/view-book?id={{book.id}}">view</a>
<a href="/update-book?id={{book.id}}">update</a>
<a href="/delete-book?id={{book.id}}">delete</a>
</br>
{% endfor %}
{% endif %}
</br>
<a href="/create-book">add book</a>
(2)vi templates/core/create-book.html
<form action="/create-book" method="post">
{% csrf_token %}
name:<input name='name'></br>
author:<input name='author'>
<input type='submit' value='save'>
</form>
(3)vi demo/templates/core/view-book.html
<table>
<tr>
<td>id</td><td>{{book.id}}</td>
</tr>
<tr>
<td>name</td><td>{{book.name}}</td>
</tr>
<tr>
<td>author</td><td>{{book.author}}</td>
</tr>
</table>
(4)vi demo/templates/core/update-book.html
<form action='/update-book' method='post'>
<input type='hidden' name='id' value='{{book.id}}'/>
{% csrf_token %}
<table>
<tr>
<td>id</td><td>{{book.id}}</td>
</tr>
<tr>
<td>author</td><td><input name='author' value='{{book.author}}'/></td>
</tr>
<tr>
<td>name</td><td><input name='name' value='{{book.name}}'/></td>
</tr>
</table>
<input type='submit'/>
</form>
9、啓動項目
python3 manage.py runserver 0.0.0.0:8080
10、中止服務
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