1 第一部分目录详解
修改django的项目当中的url中的配置:
from django.contrib import admin from django.conf.urls import url from django.urls import path from django.shortcuts import HttpResponse def home(request): return HttpResponse('<h1>hello</h1>') urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^h.html/', home), ]
需要输入对应的页面才可以访问
#2 部分 创建APP
建议做django的时候 在比较干净的目录做,不要目录嵌套目录
创建app
D:DocumentPython0404Django0425>python manage.py startapp cmdb
D:DocumentPython0404Django0425>python manage.py startapp openstack
创建完后进行一定的修改,将之前放在根下的url文件中内容进行修改:
修改如下:D:DocumentPython0404Django0425Django0425urls.py
from django.contrib import admin from django.conf.urls import url from django.urls import path from cmdb import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^h.html/', views.home), ]
修改D:DocumentPython0404Django0425cmdbviews.py
from django.shortcuts import HttpResponse def home(request): return HttpResponse('<h1>hello This is CMDB</h1>')
23:03 基本的用户名密码操作 实现简单的用户名密码登录交互界面
urls.py
"""Django0425 URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/2.0/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.conf.urls import url from django.urls import path from cmdb import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^h.html/', views.home), url(r'^login', views.login), #注意此处不要有/ ]
login.html
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 <link rel="stylesheet" href="/static/commons.css"> 7 <style> 8 label { 9 width: 80px; 10 text-align: right; 11 display: inline-block; 12 } 13 </style> 14 </head> 15 <body> 16 <form action="/login" method="post"> 17 <p> 18 <label for="username">用户名:</label> 19 <input id="username" type="test" name="user"/> 20 </p> 21 <p> 22 <label for="password">密码:</label> 23 <input id="password" type="test" name="pwd"/> 24 <input type="submit" value="提交"/> 25 <span> 26 {{ error_msg }} 27 </span> 28 29 </p> 30 </form> 31 <script src="/static/jquery-1.12.4.js"></script> 32 </body> 33 </html>
views.py
1 from django.shortcuts import render 2 3 # Create your views here. 4 from django.shortcuts import HttpResponse 5 6 from django.shortcuts import render 7 from django.shortcuts import redirect 8 9 def login(request): 10 #包含用户提交的所有信息 11 # f=open('templates/login.html','r',encoding='utf-8') 12 # data=f.read() 13 # f.close() 14 # return HttpResponse(data) 15 16 print(request.method) #获取用户的提交方式 17 error_msg='' 18 if request.method=="POST": 19 #1 普通写法 20 # user=request.POST['user'] 21 # pwd=request.POST['pwd'] 22 #2 较好的写法 相比较第一种如果获取不到会报错 第二种 23 #不会 24 user=request.POST.get('user',None) 25 pwd=request.POST.get('pwd',None) 26 print('