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

  • 相关阅读:
    Collection集合总结
    JAVA泛型通配符T,E,K,V区别,T以及Class,Class的区别【转】
    Runnable和Thread
    jdk自带的函数式接口(配合lambda使用)
    java8-接口的静态方法(static)和默认方法(default)
    Java 8
    java8-Stream流【转】
    java8-lambda表达式
    Oracle:ORA-01461报错
    ASP.NET Core 入门笔记2,建立项目
  • 原文地址:https://www.cnblogs.com/someoneHan/p/4639521.html
Copyright © 2011-2022 走看看