登录示例
form表单提交数据的三个关键
form标签必须要有action和method属性
form标签中获取用户输入的标签必须要有name属性
form标签中必须要有submit按钮
request
request.POST --> 所有POST请求提交的数据(类似一个大字典)
request.POST['pwd']
request.POST.get('pwd') --> 推荐使用这个
基础必会三件套
HttpResponse --> 返回简单的字符串
render --> 返回HTML页面
redirect --> 返回一个重定向(跳转)
request.method --> 获取当前请求的方法
render方法传递额外的参数
render(request, 'xx.html', {'a': 'aaa', 'b': 'bbb'})
本质上就是一个字符串替换。
Django app
创建APP --> python manage.py startapp app01
在settings.py中告诉Django,我现在有了一个app
现阶段(Django基础)创建Django项目的步骤
PyCharm或者命令行创建一个Django项目
django-admin startproject 项目名
创建一个初始的app,并且在settings.py中告诉Django
python manage.py startapp app的名字
检查settings.py 专门存放HTML文件的Templates配置项
配置静态文件相关
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'), ]
在项目的根目录创建一个用来存放静态文件的static目录
注释csrf相关的那一行(大概在46行)
否则表单没办法提交数据,就会提示Forbidden 403的错误
登录示例步骤
#1.建立django #2.检查templates用来存放html文件 #3.检查创立 一个static用来存放样式 #4.注释csrf相关的那一行(46行) #5.检查html登录文件的form表单 form标签必须要有action和method属性 form标签中获取用户输入的标签必须要有name属性 form标签中必须要有submit按钮 #6.完成代码 request.POST --> 所有POST请求提交的数据(类似一个大字典) request.POST.get('pwd') --> 获取用户输入的密码 request.method --> 获取当前请求的方法 render方法传递额外的参数,本质上就是一个字符串替换 redirect --> 返回一个重定向(跳转)
#urls代码: """mysiteday56 URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.11/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) """ from django.conf.urls import url from django.contrib import admin from django.shortcuts import render, HttpResponse, redirect def login(request): # 如果是POST提交数据 error_msg = '' if request.method == 'POST': print(request.POST) email = request.POST['email'] pwd = request.POST.get('pwd', None) print('-' * 120) # 2. 判断邮箱和密码是否正确 if email == '[email protected]' and pwd == 'xyh1234': # 2.1 如果正确,我就回个登陆成功 # return HttpResponse('登陆成功') # 打开index.html # return render(request, 'index.html') # return index(request) # # return redirect('/index/') return redirect('https://www.luffycity.com') else: # 2.2 如果失败,返回错误提示 # 打开login.html文件 # 读取内容 # 进行字符串替换,把特殊符号替换成我指定的变量 error_msg = '用户名或密码错误' return render(request, 'login.html', {'error_msg': error_msg}) def index(request): return render(request, 'index.html') # 定义一个专门用来接收登录页面请求的函数 # def xyh(request): # # request:所有和请求相关的数据都封装到这个对象里了 # # 1. 获取用户输入的内容 # print(request.POST) # email = request.POST['email'] # pwd = request.POST.get('pwd', None) # print('-' * 120) # # 2. 判断邮箱和密码是否正确 # if email == '[email protected]' and pwd == 'xyh1234': # # 2.1 如果正确,我就回个登陆成功 # # return HttpResponse('登陆成功') # # 打开index.html # # return render(request, 'index.html') # # return index(request) # # # # return redirect('/index/') # return redirect('https://www.luffycity.com') # else: # # 2.2 如果失败,返回错误提示 # return HttpResponse('用户名或密码错误!') urlpatterns = [ url(r'^login/', login), url(r'^index/', index), ]
app相关
一个Django项目可以分为很多个APP,用来隔离不同功能模块的代码。
命令行创建
python manage.py startapp app名字
#urls代码: from django.conf.urls import url from django.contrib import admin from app01.views import login, index urlpatterns = [ url(r'^login/', login), url(r'^index/', index), ] #app01-views的代码(用来存放函数): from django.shortcuts import render # Create your views here. from django.shortcuts import render, HttpResponse, redirect def login(request): # 如果是POST提交数据 error_msg = '' if request.method == 'POST': print(request.POST) email = request.POST['email'] pwd = request.POST.get('pwd', None) print('-' * 120) # 2. 判断邮箱和密码是否正确 if email == '[email protected]' and pwd == 'xyh1234': # 2.1 如果正确,我就回个登陆成功 # return HttpResponse('登陆成功') # 打开index.html # return render(request, 'index.html') # return index(request) # return redirect('/index/') # return redirect('https://www.luffycity.com') else: # 2.2 如果失败,返回错误提示 # 打开login.html文件 # 读取内容 # 进行字符串替换,把特殊符号替换成我指定的变量 error_msg = '用户名或密码错误' return render(request, 'login.html', {'error_msg': error_msg}) def index(request): return render(request, 'index.html')