Python的Django

Posted 90500042陈

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python的Django相关的知识,希望对你有一定的参考价值。

 

 

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),
]
View Code

 

 

 需要输入对应的页面才可以访问

 

 

 

 

 

 

 #2 部分   创建APP

 

     建议做django的时候  在比较干净的目录做,不要目录嵌套目录

 

创建app

D:\\Document\\Python0404\\Django0425>python manage.py startapp cmdb

D:\\Document\\Python0404\\Django0425>python manage.py startapp openstack

 

 

创建完后进行一定的修改,将之前放在根下的url文件中内容进行修改:

修改如下:D:\\Document\\Python0404\\Django0425\\Django0425\\urls.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:\\Document\\Python0404\\Django0425\\cmdb\\views.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>
View Code

 

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(\'\\033[31;1muserinfo:%s\\033[0m\'%user)
27         print(\'\\033[33;1mpasswdinfo:%s\\033[0m\'%pwd)
28         if user==\'nod\' and pwd==\'nod\':
29             #引入redirect重定向
30             return redirect(\'http://www.baidu.com\')
31         else:
32             error_msg=\'你输入的账户密码不正确,请重新输入\'
33 
34     return render(request,\'login.html\',{\'error_msg\':error_msg})
35 
36 def home(request):
37     return HttpResponse(\'<h1>hello This is CMDB</h1>\')
View Code

 

注意settings里需要修改的配置

 

以上是关于Python的Django的主要内容,如果未能解决你的问题,请参考以下文章

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段

如何在 Django Summernote 中显示编程片段的代码块?

Django REST框架--认证和权限

常用python日期日志获取内容循环的代码片段

python 有用的Python代码片段

使用 Django 模板作为片段