先写简单的:
views.py:
from django.shortcuts import HttpResponse from django.shortcuts import render def login(request): return render(request, "login.html") def home(request): return HttpResponse("urls.py:你好
")
from django.shortcuts import HttpResponse from django.shortcuts import render def login(request): return render(request, "login.html") def home(request): return HttpResponse("templates下建立login.html你好
")
html>
lang="en">
charset="UTF-8">
Title
运行:
如果有CSS和JS(静态文件),会出错,那么接下来配置:
创建了一个static文件夹,放入JS和CSS文件,然后在HTML文件中修改下:
html>
lang="en">
charset="UTF-8">
Title
rel="stylesheet" href="/static/commons.css">
在settings.py后面加上这段:
STATICFILES_DIRS={ os.path.join(BASE_DIR, "static"), }
再次运行:
由此可以得出:
应该优先
1.配置模板路径(在settings.py的TEMPLATES里的‘DIRS’)
2.配置静态目录(见上)
静态文件处理好了,那么点击提交后到底提交到了哪里呢?
继续修改,完成用户名密码验证:
注释掉setting.py里面的:
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', ]
views.py修改如下:
from django.shortcuts import HttpResponse from django.shortcuts import render from django.shortcuts import redirect def login(request): error_msg = "" if request.method == "POST": user = request.POST.get("user", None) pwd = request.POST.get("pwd", None) if user == "yiqing" and pwd == "handsome": return redirect("http://www.baidu.com") else: error_msg = "用户名或密码错误" return render(request, "login.html", {"error_msg": error_msg}) def home(request): return HttpResponse("你好
")
login.html修改如下:
html>
lang="en">
charset="UTF-8">
Title
rel="stylesheet" href="/static/commons.css">
如果输入用户名和密码正确,则会跳转百度,否则告诉用户错误: