命令行下创建
pip install django
命令
d:/django-admin startproject mysite
切换到目录下
d:/mysite
D:\mysite>python manage.py runserver 127.0.0.1:4444
pycharm:
目录结构
Myweb
-settings.py #配置文件
-url.py #路由文件url->函数
-wsgi #定义socket,wsgiref,uwsgi
-manage.py #运行文件
创建project-->配置
静态文件配置
#settings.py文件下
#默认前缀
STATIC_URL = '/static/'
STATICFILES_DIRS=(
os.path.join(BASE_DIR,'static'),
)
屏蔽跨站
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
简单访问请求
#url.py
from django.http import HttpResponse
from django.urls import path
def test(request):
return HttpResponse('1111111111')
urlpatterns = [
path('index/', index),
]
访问模板
#url.py
from django.http import HttpResponse
from django.urls import path
from django.shortcuts import render, redirect
def test(request):
return render(request, 'test.html')
urlpatterns = [
path('index/', index),
]
#默认模板地址设置,在settings.py下
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
简单登录示例:
#url.py文件
from django.contrib import admin
from django.http import HttpResponse
from django.http import HttpRequest
from django.shortcuts import render, redirect
from django.urls import path
def login(request):
if request.method == 'GET':
return render(request, 'login.html')
else:
u = request.POST.get('user')
p = request.POST.get('pwd')
if u == 'root' and p == '123456':
return redirect('/index/')
else:
return render(request, 'login.html', {'msg': '账密错误'})
def index(request):
return render(request, 'index.html')
urlpatterns = [
path('login/', login),
path('index/', index),
]
#login.html文件
<!DOCTYPE html>
<html lang="en">
<thead>
<meta charset="UTF-8">
<title>用户登录</title>
</thead>
<link rel="stylesheet" href="/static/1.css"/>
<body>
<h1>用户登录</h1>
<form METHOD="post" action="/login/">
<input type="text" name="user">
<input type="password" name="pwd">
<input type="submit" value="登录">
</form>{{ msg }}
</body>
</html>
模板和特殊标记
def index(request):
return render(
request,
'index.html',
{
'name':'tom',
'phone': ['10086', '12580'],
'info': {'addr': 'gd', 'age': 18},
'user_list_dict':[
{'id':1,'name':'tom','email':'tom@qq.com'},
{'id':2,'name':'mary','email':'mary@qq.com'},
{'id':3,'name':'jimmy','email':'jimmy@qq.com'},
]
}
)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
{{ name }}
{{ phone.0 }}
{{ phone.1 }}
{% for item in phone %}
{{ item }}
{% endfor %}
{{ info.addr }}
{{ info.age }}
{% for item in info.items %}
{{ item.0 }}{{ item.1 }}
{% endfor %}
<br>
<table border="1">
{% for row in user_list_dict %}
<tr>
<td>{{ row.id }}</td>
<td>{{ row.name }}</td>
<td>{{ row.email }}</td>
</tr>
{% endfor %}
</table>>
</body>
</html>