Django practice (4): scaffold Product Analysis

Source: Internet
Author: User

in the previous section, we used a plug-in to generate a rails-like scaffold, which is nothing more than urlconf + MTV. Let's see what is generated.
first, the "entry" definition is urlconf. Enable URLs. py:

FromDjango. conf. URLs. defaultsImportPatterns, include, URLFromDepot. ViewsImportHellourlpatterns= Patterns ('', URL (R'^ Hello/Hello ),) Urlpatterns+ = Patterns ('', (R'^ Depotapp/', Include ('Depotapp. URLs')),)

 

The aboveCodeThe added configuration line indicates that the URL starting with depotapp is processed by the depotapp/URLs. py file.

In the Django URL configuration, in addition to the (Regular Expression, view function) method, the (Regular Expression, include file) method is also supported. Generally, the URL related to the app is written to its own URL configuration file and referenced in the project.

Next, let's take a look at the generated depotapp/URLs. py content:

 From Django. conf. URLs. defaults Import * From Models Import * From Views Import * Urlpatterns = Patterns ( ''  , (R '  Product/create/$  '  , Create_product), (R  '  Product/LIST/$  '  , List_product), (R  '  Product/edit /(? P [^/] +)/$  '  , Edit_product), (R  '  Product/View /(? P [^/] +)/$  '  , View_product ),) 

 

Map the CRU (no D) URL to the view. The view is defined in depotapp/views. py:

 From Django Import  Forms  From Django. Template Import  Requestcontext  From Django. HTTP Import  Httpresponse, httpresponseredirect  From Django. template. Loader Import  Get_template  From Django. Core. paginator Import  Paginator  From Django. Core. urlresolvers Import  Reverse  #  APP specific files  From Models Import * From Forms Import * Def  Create_product (request): Form = Productform (request. PostOr  None)  If  Form. is_valid (): form. Save () Form = Productform () T = Get_template ( '  Depotapp/create_product.html  '  ) C = Requestcontext (request, locals ())  Return  Httpresponse (T. Render (c ))  Def List_product (request): list_items = Product. Objects. All () paginator = Paginator (list_items, 10 )  Try  : Page = Int (request. Get. Get ( '  Page  ' , '  1  '  ))  Except  Valueerror: Page = 1Try  : List_items = Paginator. Page (page)  Except  : List_items = Paginator. Page (paginator. num_pages) T = Get_template ( '  Depotapp/list_product.html  '  ) C = Requestcontext (request, locals ())  Return  Httpresponse (T. Render (c )) Def  View_product (request, ID): product_instance = Product. Objects. Get (ID = ID) T = Get_template ( '  Depotapp/view_product.html  '  ) C = Requestcontext (request, locals ())  Return  Httpresponse (T. Render (c ))  Def  Edit_product (request, ID): product_instance = Product. Objects. Get (ID =ID) Form = Productform (request. Post Or None, instance = Product_instance)  If  Form. is_valid (): form. Save () T = Get_template ( '  Depotapp/edit_product.html  '  ) C = Requestcontext (request, locals ())  Return Httpresponse (T. Render (c ))

 

There are many related content in the view, mainly templates, followed by model classes, paginator splitters, form forms, and so on.

it covers typical web application interaction content.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.