zoukankan      html  css  js  c++  java
  • 07-Django视图进阶

    1、调试模式

    Django项目下的settings.py 默认是DEBUG=True,开发的时候一般要开启调试模式,当项目完成发布必须要改成False,否则会暴露网站的配置信息,修改以下两行:

    # DEBUG=True
    # ALLOWED_HOST=[]   # 表示允许哪些ip地址访问
    
    DEBUG=True
    ALLOWED_HOST=['*']    # 表示允许所有网站访问

    如:404 则会显示 Not Found

    2、错误视图

    1)关闭调式模式后,默认会显示一个标准的错误页面,如果要显示自定义的页面,只需要在templates下自定义一个404.html ,不需要进行url的配置,Django会自动匹配

    出现的原因一般是地址没有配置或配置出错

    2) 500 服务端错误,一般为视图里代码出错
    标准的显示页面未 Server Error(500),也可以自定义500页面,在templates下创建一个500.html

    3、捕获url参数

    进行url匹配时,把所需要捕获的部分设置成一个正则表达式组,这样django框架就会自动把匹配成功后的相应组的内容作为参数传递给视图函数.

    1)位置参数:参数名可以随意指定

    url(r'^delete(d+)$',views.delete)  # 位置参数
    
    
    def delete(request,bid):
        return HttpResponse(bid)
    

      

    2)关键字参数:在位置参数的基础上 给正则表达式组命名即可
    ?P<组名>
    关键字参数,视图中参数名称必须和正则表达式组名一致

    url(r'^delete(?P<num>d+)$',views.delete)  # 关键字参数
    
    def delete(request,num):    # 视图中参数名称必须和正则表达式组名一致 都是num
        return HttpResponse(num)

    4、登录案例

    def delete(request,num):   
        return HttpResponse(num)

    参数中request就是HttpRequest类型对象,request包含浏览器请求的信息
    思路:
    1、需要一个登陆页面 login.html
    2、需要一个登陆后页面,如index.html
    3、在views.py中 定义二个视图函数,一个是登录页面,一个是获取登录信息并做校验

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>登录</title>
    </head>
    <body>
    <--
    form表单必须有的参数:
        method:post或者get,post安全
        action 必须要有规定当提交表单时,向何处发送表单数据
    -->
    <form  method="post"  action="/login_check">
        用户名:<input type="text"  name="username"/><br/>
        密码: <input type="password" name="password"/><br/>
        <input type="submit" value="提交"/>
    </form>
    </body>
    </html>
    

      

    def my_render(request,template_path,context_dict):
    
        temp = loader.get_template(template_path)
        # context=RequestContext(request,context_dict)
        context=context_dict
        res_html=temp.render(context)
        return HttpResponse(res_html)
    

     

    def login(request):
        return my_render(request,'booktest/login.html',{})
    

     

    def login_check(request):
        """
            request.POST 保存的是post提交的参数
            request.GET 保存的是get提交的参数
        """
        
        # 1. 获取提交的用户名和密码
        # print(type(request.POST))   # 类型QueryDict类型对象,跟字典相似
        username = request·POST.get('username')
        password = request·POST.get('password')
        
        # 2. 进行登录校验并响应应答,实际情况用户名和密码是保存在数据库里的
        if username=='admin'  and password =='123456' :
            # 用户名和密码正确,跳转到首页
            return HttpResponseRedirect('/index')
            
        else:
            # 用户名和密码错误,跳转登录页面
            return HttpResponseRedirect('/login')
            
    

      

     

     

    
    
  • 相关阅读:
    HDU 5486 Difference of Clustering 图论
    HDU 5481 Desiderium 动态规划
    hdu 5480 Conturbatio 线段树 单点更新,区间查询最小值
    HDU 5478 Can you find it 随机化 数学
    HDU 5477 A Sweet Journey 水题
    HDU 5476 Explore Track of Point 数学平几
    HDU 5475 An easy problem 线段树
    ZOJ 3829 Known Notation 贪心
    ZOJ 3827 Information Entropy 水题
    zoj 3823 Excavator Contest 构造
  • 原文地址:https://www.cnblogs.com/wysk/p/11447356.html
Copyright © 2011-2022 走看看