用pychram创建Django程序
-
file->New Project
python manage.py startapp app01
-
配置settings.py 文件
最后一行添加以下:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
- 修改urls.py
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', views.index),
]
- 修改views.py
from django.shortcuts import render,HttpResponse
# Create your views here.
def index(request):
return HttpResponse('Index')
-
基本程序效果图:
@进一步
- 修改urls.py
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', views.index),
url(r'^login/', views.login),
]
- 修改views.py
from django.shortcuts import render,HttpResponse,redirect
# Create your views here.
def index(request):
return HttpResponse('Index')
def login(request):
# 判断用户获取数据方式是GET,就返回什么数据
if request.method == "GET":
return render(request, 'login.html')
# 判断用户获取数据方式是POST,就判断用户提交的数据是否正确
elif request.method == "POST":
u = request.POST.get('user')
p = request.POST.get('pwd')
if u == 'alex' and p == '123':
return redirect('/index/')
else:
return render(request, 'login.html')
else:
# PUT,DELETE,HEAD,OPTION...
return redirect("/index/")
- 修改login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/login/" method="post">
<p>
<input type="text" name="user" placeholder="用户名">
</p>
<p>
<input type="password" name="pwd" placeholder="密码">
</p>
<input type="submit" value="提交">
</form>
</body>
</html>
-
效果图: