zoukankan      html  css  js  c++  java
  • django views

    A view is a "type" of web page in the Django application that generally serves a specific function and has a specific template,for example you might have the following views:

    1. Blog homepage--displays the latest few entries

    2. Entry "details" page--permalink page for a single entry

    3. year-base archive page--display all months with entries in the given year

    4. month-based archive page -- display all days with entries in the given month

    5. day-based archive page -- display all entries in the given day

    6 comment action -- handles posting comments to a given entry

    In Django, web pages and other content are delivered by views,Each view is represented by a simple python function,Django will choice a view by examing the url that's requested

    Write The first view

    In the view.py and put some code like this

    from django.http.response import HttpResponse
    def index(request):
        return HttpResponse("Hello,world,you are at the poll index")
    View Code

    II in the polls directory, create a file called urls.py and in the urls.py type code like this

    from django.conf.urls import patterns,url, include
    from polls import views
    
    urlpatterns = patterns('',
        url(r'^$',views.index,name='index')
        )
    View Code

    III the next step is to point the root URLconf at the mysite/urls.py insert an include() leave us with

    urlpatterns = patterns('',
        url(r'^polls', include('polls.urls')),
        url(r'^admin/', include(admin.site.urls)),
    )
    View Code

    for now in the browser we can see the words "Hello,world,you are at the poll index"

    The url() function takes four arguments, two required:reges and view,and tow optional:kwargs and name

    url() argument:regex

      the term "regex" is a commonly used short form meaning "regular expression",which is a syntax for matching patterns in strings,or in this case,url patterns,Django starts at the first regular expression and makes its way down the list, comparing the requested URL againse each regular expression until if finds one that matches.

      Note that these resular expressions do not search GET and POST parameters or the domain name,for example,in a request to http://www.example.com/myapp, the URL conf will look for myapp., in a request to http://www.example.com/myapp/?page = 3,the url conf will alse look for myapp/

          fienally, a performace note: these regular expressions are compiled the first time the URL conf module is loaded, they are super fast

    url() argument:view

    when django finds a regular expression match, Django calls the specified view function,with an HttpRequest object as the first argument and any 'captured' values from the regualr espression as other arguments,

    url() argument:kwargs

    Arbitrary keyworld arguments can be passed in a dictionary to the target view

    url() argument:name

    Naming you url lets you refer to it unambiguously from elsewhere in Django expecially templates, this powerful feature allows you to make global changes to the urls patterns of your project while only touching a single file

    Write more views

  • 相关阅读:
    Hybrid APP基础篇(三)->Hybrid APP之Native和H5页面交互原理
    Hybrid APP基础篇(二)->Native、Hybrid、React Native、Web App方案的分析比较
    Hybrid APP基础篇(一)->什么是Hybrid App
    JavaScript筑基篇(一)->变量、值与对象
    深入Node.js的进程与子进程:从文档到实践
    深入Node模块Buffer-学会操作二进制
    深入Nodejs模块fs
    刷《一年半经验,百度、有赞、阿里面试总结》·手记
    Asp.Net Core 轻松学-被低估的过滤器
    css精灵图&字体图标
  • 原文地址:https://www.cnblogs.com/someoneHan/p/4639521.html
Copyright © 2011-2022 走看看