Django framework document parsing --views.py

views 

from django.shortcuts Import render
 # packet shortcuts from the shortcut django, render load module is supplied 

from booktest.models Import the BookInfo, HeroInfo 

# # shortcuts package, the package contents render the tools and workflow: 
# DEF my_render (Request , template_path, context_dict = {},): 
#      "" "process using the template file" "" 
# 
#      # 1. Setting the project file by loading the template directory, django using the template method get_template package template tool loader, load a template file, and use a variable to receive. 
#      the TEMP = loader.get_template (template_path) # in the project, will create an application with the same name for each folder in the application folder template template files, put all the application template. 
# 
#     2. The definition of the context template #: transmitting data to a template, and using the same tool loader package, RequstContext tool response context, incoming request parameters, and other data need to put in the dictionary, the same variables received. 
#      Context the RequestContext = (request , context_dict) # the value of the blank pass without 
# 
#      # 3. rendering template: the conversion template variable, generate standard html content using a template instance object returned, calling the render method, the incoming object is returned in response to the context. variable receiving 
#      res_html = temp.render (context) # returns all the contents of the page. 
# 
#      # 4. return the data to the browser. end view of the call, using the HttpResponse return to the page content. 
#      return HttpResponse (res_html) 




from django.template Import Loader, RequestContext 

# the Create your views here Wallpaper. 

from django.http Import HttpResponse
 #  Import HttpResponse page corresponding module from django frame http packets 
# when url meet the requirements, view the corresponding content back. 


DEF index (Request):
     # return HttpResponse ( 'index') 

    # my_render (Request, "booktest / index .html ") 
    return the render (Request,
            " booktest / index.html " , 
           { " content " : " this is a database of content " ,
             " List " : List (Range (1,10 ))} 
           ) 


DEF index2 ( Request):
     return HttpResponse ( ' this is the second Home! ')


# Create a new module processes: 
#      0. define the view 
#      1. Define the url 
#      2. Import models corresponding module, the incoming data. 
#      3. Create a template 
#      4. Using the render, the incoming request, add context template ., binding data rendering template, return to the browser 
#      5 return value. 

DEF show_books (Request): 
    Books = BookInfo.objects.all ()
     return the render (Request, " booktest / show_books.html " , { ' Books ' : } Books) 

DEF the Detail (Request, Bid):
     # accordance id returned books again query 
    # book = BookInfo.objects.get (Bid) 
    #Here you need to be populated with the id variable, the variable no way be seen wearing come to find 
    Book = BookInfo.objects.get (id = Bid) 
    Heros = book.heroinfo_set.all ()
     return the render (Request, " booktest / detail.html " , {
         " Heros " : Heros,
         " Book " : Book, 
    })

 

views

from django.shortcuts Import render
# django package from shortcuts shortcut, the render load module is supplied

from the BookInfo booktest.models Import, HeroInfo

# # shortcuts package, the package contents render the tools and workflow:
# DEF my_render (Request , template_path, context_dict = {},):
# "" "process using the template file" ""
#
# # 1. Setting the project file by loading the template directory, django using the template method get_template package template tool loader, load a template file, and use a variable to receive.
# = loader.get_template the TEMP (template_path) # in the project, will create an application with the same name for each folder in the application folder template template files, put all the application template.
#
2. the template ## is defined context: transmitting data to a template, and using the same tool loader package, RequstContext tool response context, incoming request parameters, and other data need to put in the dictionary, the same variables received.
# = the RequestContext context ( request, context_dict) # the value of the blank need not pass
#
# # 3. Rendering template: the conversion template variable, generate standard html content using a template instance object returned, calling the render method, the incoming object in response to receiving the context variables returned..
# Res_html = temp.render (context) # Back to all content.
#
# # 4. return the data to the browser. end view of the call, using the HttpResponse return to the page content.
# return HttpResponse (res_html)




from django.template Import Loader, RequestContext

# the Create your views here Wallpaper.

from Django. http import HttpResponse
. # import HttpResponse page corresponding module from django frame http package
when # when url meet the requirements, view the corresponding content back.


DEF index (Request):
    # return HttpResponse ( 'index')

    # my_render (Request, "booktest / index.html")
    return the render (Request,
           "booktest / index.html",
           { "content": "this is a database of content",
            "List":list(range(1,10))}
           )


DEF index2 (Request):
    return the HttpResponse ( '! This is the second home')


# Create a new module processes:
# 0. define the view
. # 1. Define the url
# 2. Import models corresponding module, incoming data .
# 3. Create a template
. # 4. using the render, the incoming request, add context as a template, the template binding data rendering, returned to the browser
# 5 return value.

DEF show_books (Request):
    Books = BookInfo.objects.all ()
    return the render (Request, "booktest / show_books.html", { 'Books': Books})

DEF Detail (Request, Bid):
    # re-query based on the id returned books
    # book = BookInfo.objects.get (bid)
    # id here need to assign a variable, the variable no way be seen wearing come to find
    Book = BookInfo.objects.get (id = Bid)
    Heros = book.heroinfo_set.all ()
    return the render (Request, "booktest / the Detail. html ", {
        "heros":heros,
        "book":book,
    })

Guess you like

Origin www.cnblogs.com/jrri/p/11492053.html