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.