整个过程适合有MVC基础的程序员
创建一个项目:
django-admin.py startproject HelloWorld
运行一个项目:
python manage.py runserver 0.0.0.0:8000
修改django配置:
vim settings.py
ALLOWED_HOSTS = ['*']
测试是否成功:
10.1.8.34:8000
配置url:在目录HelloWorld/HelloWorld
vim urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from . import view
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^hello$', view.hello), ##浏览器访问hello时指向view.hello函数
]
编辑view:在目录HelloWorld/HelloWorld
vim view.py
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world ! ")
测试是否成功:
10.1.8.34:8000/hello
创建blog网站和article网站:
manage.py startapp blog
manage.py startapp article
注册创建的网站:
vim settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'article',
]
每个网站单独管理自己的网址:
vim urls.py ##在HelloWorld/HelloWorld 目录下
from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from . import view
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^hello$', view.hello),
path('blog/', include('blog.urls')),#包含blog网站的url路径
path('article/', include('article.urls')),#包含article网站的url路径
]
blog网站下url管理:
vim urls.py ##blog目录下
from django.conf.urls import url
from blog import views
urlpatterns = [
url(r'^hello/$', views.hello),
]
article网站下url管理:
vim urls.py ##article目录下
from django.conf.urls import url
from article import views
urlpatterns = [
url(r'^hello/$', views.hello),
]
模板使用:以blog网站为例
mkdir template #在blog目录下建立template目录
vim hello.html#在template目录下创建
<h1>Hello template</h1>
vim views.py #修改blog目录下文件
from django.http import HttpResponse
from django.template import loader
def hello(request):
t = loader.get_template('hello.html')
d = {}
return HttpResponse(t.render(d))
模板变量使用:以blog为例
vim views.py #blog目录下
from django.http import HttpResponse
from django.template import loader
def hello(request):
t = loader.get_template('hello.html')
d = {"name":"lidonghai","age":29,"sex":"mail"} ##变量放到dict中
return HttpResponse(t.render(d))
vim hello.html #blog目录下的templates目录下
<h1>Hello blog template</h1>
name:{{name}},age:{{age}},sex:{{sex}}
模板标签使用+过虑器使用:以blog为例
vim hello.html #blog目录下的templates目录下
<h1>Hello blog template</h1>
name:{{name}},age:{{age}},sex:{{sex}}</br>
another one {{user.name}},{{user.age}},{{user.sex}}
{% if age > 19 %}
<li>成年</li>
{% else %}
<li>no成年</li>
{% endif %}
<!-- not 可以判断不存在、为空、为0 -->
{% if not user1 %}
<li>user1不存在</li>
{% endif %}
{% for i in book %}
<li>{{ i }}</li>
<li>模板过虑器:{{ i | upper | lower }}</li>
<li>forloop.counter:{{forloop.counter}}</li>
<li>forloop.counter0:{{forloop.counter0}}</li>
<li>forloop.revcounter:{{forloop.revcounter}}</li>
<li>forloop.revcounter0:{{forloop.revcounter0}}</li>
{% endfor %}
{% for i in book %}
{% if forloop.first %}
<li>第一本书</li>
{% endif %}
<li>{{i}}</li>
{% if forloop.last %}
<li>最后一本书</li>
{% endif %}
{% endfor %}
{% for i in book2 %}
<li>{{i}}</li>
{% empty %}
<li>nothing is here</li>
{% endfor %}
{% for key,value in frult.items %}
<li>{{key}}:{{value}}</li>
{% endfor %}
#模板过虑器自定义:以blog网站为例
mkdir templatetags ##blog目录下新建目录
vim __init__.py ## templatetags目录下新建文件,表示 templatetags是包
vim percent.py ##templatetags目录下新建文件,这个就是过虑器名字
from django import template
register = template.Library()
def percent(value):
return str(value) + "%"
register.filter(percent)
vim hello.html ##templates目录下使用过虑器
{% load percent %} ##使用前要加载
<li> age percent is: {{ user.age | percent }}</li>
##GET、POST传参:以blog网站为例
GET:
vim views.py ##blog目录下增加以下代码
from django.shortcuts import render_to_response
def search(request):
return render_to_response('search.html')
def search_submit(request):
request.encoding='utf-8'
if 'q' in request.GET:
message = '你搜索的内容为: ' + request.GET['q']
else:
message = '你提交了空表单'
return HttpResponse(message)
vim urls.py ##blog目录下urls.py如下
from django.conf.urls import url
from blog import views
urlpatterns = [
url(r'^hello/$', views.hello),
url(r'^search/$', views.search),
url(r'^search_submit/$', views.search_submit),
]
vim search.html ##blog目录下templates目录下编辑
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<form action="/blog/search_submit" method="get">
<input type="text" name="q">
<input type="submit" value="搜索">
</form>
</body>
</html>
POST:
vim views.py ##blog目录下增加以下代码
def post_search(request):
return render_to_response('post_search.html')
def post_search_submit(request):
request.encoding='utf-8'
if 'q' in request.POST:
message = 'post你搜索的内容为: ' + request.POST['q']
else:
message = 'post你提交了空表单'
return HttpResponse(message)
vim urls.py ##blog目录下urls.py如下
from django.conf.urls import url
from blog import views
urlpatterns = [
url(r'^hello/$', views.hello),
url(r'^search/$', views.search),
url(r'^post_search/$', views.post_search),
url(r'^search_submit$', views.search_submit),
url(r'^post_search_submit$', views.post_search_submit),
]
vim post_search.html ##blog目录下templates目录下编辑
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<form action="/blog/post_search_submit" method="post">
<input type="text" name="q">
<input type="submit" value="搜索">
</form>
</body>
</html>
vim settings.py ##HelloWorld/HelloWord目录下修改,注释蓝色行。post可以用了,但是不太安全。
浏览器输入验证:http://10.1.8.34:8000/blog/post_search