7 Stark configuration, generate 2-tier URLs

Source: Internet
Author: User

1. Stark Component Development

Stark component development, imitation Django comes with the admin component.

1. Django Admin Configuration

Model
 fromDjango.contribImportAdmin#Register your models here. from. ModelsImport*classuseradmin (admin. Modeladmin): List_display= ["PK",'name',' Age'] List_filter= ['name',' Age']    #customizing action specific methods    deffunc (Self,request,queryset): Queryset.update ( Age=44) Func.short_description="Bulk Initialization Operations"Actions=[Func]admin.site.register (Userinfo,useradmin) admin.site.register (book)
admin.py
Makemigrationsmigratecreatesuperuser (Alex 1234qwer)

2, Starkadmin Register

1. Startapp Stark

stark/service/stark.py (single case)
classModelstark (object):"""Book,userinfo Object"""    def __init__(self, model, site): Self.model=Model Self.site=siteclassStarksite (object):"""Create class for site Singleton object"""    def __init__(self): Self._registry= {}    defRegister (Self,model, stark_class=None):if  notStark_class:stark_class=Modelstark Self._registry[model]=Stark_class (model,self) site= Starksite ()
View Code


2. Configuration in Settings

 from Import AppConfig  from Import Autodiscover_modules class App01config (AppConfig):     ' APP01 '    # when the program starts, it scans the app for the specified file (starkadmin.py) and executes    def Ready (self):        autodiscover_modules ('starkadmin')

3. Create file starkadmin: Copy admin file

app01/starkadmin.py

4. URL

2, generate URL1, 2 layer URL distribution of the primary version

 fromDjango.conf.urlsImportURL fromDjango.shortcutsImportHttpResponseclassModelstark (object):"""Book,userinfo Object"""    def __init__(self, model, site): Self.model=Model Self.site=siteclassStarksite (object):"""Create class for site Singleton object"""    def __init__(self): Self._registry= {}    defRegister (Self,model, stark_class=None):if  notStark_class:stark_class=Modelstark Self._registry[model]=Stark_class (model,self)defList_view (self,request):returnHttpResponse ('List_view')    defAdd (self,request):returnHttpResponse ('Add')    defDelete (self, request, ID):returnHttpResponse ('Delete')    defChange (self, request, ID):returnHttpResponse (' Change')    defget_urls2 (self):"""Construction Add/delete/change"""Temp=[] temp.append (URL (r'^$', Self.list_view)) Temp.append (URL (r'^add/', Self.add)) Temp.append (URL (r'^ (\d+)/delete/', Self.delete)) Temp.append (URL (r'^ (\d+)/change/', Self.change)) returnTemp @propertydefurls2 (self):returnself.get_urls2 (), none, nonedefGet_urls (self):"""Construction Urls:app01/book"""Temp= []        Print(self._registry) forModel, Stark_class_objinchSelf._registry.items (): Model_name=model._meta.model_name App_label=Model._meta.app_label#distribute additions and deletions and change the searchTemp.append (URL (r'^%s/%s/'%(App_label, Model_name), SELF.URLS2)) returnTemp @propertydefURLs (self):returnself.get_urls (), None, Nonesite= Starksite ()
View Code

2, how to ensure that the different model models of the table corresponding to different page display

3, Modelstark
Modelstark ()
Self.model: Model table for user access

  

4, two distribution optimized version
 fromDjango.conf.urlsImportURL fromDjango.shortcutsImportHttpResponseclassModelstark (object):"""Book,userinfo Object"""    def __init__(self, model, site): Self.model=Model Self.site=sitedefList_view (self, request):Print(Self.model)#<class ' App01.models.Book ' > user-accessible model table        returnHttpResponse ('List_view')    defAdd (self, request):returnHttpResponse ('Add')    defDelete (self, request, ID):returnHttpResponse ('Delete')    defChange (self, request, ID):returnHttpResponse (' Change')    defget_urls2 (self):"""Construction Add/delete/change"""Temp=[] temp.append (URL (r'^$', Self.list_view)) Temp.append (URL (r'^add/', Self.add)) Temp.append (URL (r'^ (\d+)/delete/', Self.delete)) Temp.append (URL (r'^ (\d+)/change/', Self.change)) returnTemp @propertydefurls2 (self):returnself.get_urls2 (), none, noneclassStarksite (object):"""Create class for site Singleton object"""    def __init__(self): Self._registry= {}    defRegister (Self,model, stark_class=None):if  notStark_class:stark_class=Modelstark Self._registry[model]=Stark_class (model,self)defGet_urls (self):"""Construction Urls:app01/book App01/userinfo"""Temp= []        Print(self._registry) forModel, Stark_class_objinchSelf._registry.items (): Model_name=model._meta.model_name App_label=Model._meta.app_labelPrint(Model,'---->', Stark_class_obj)"""<class ' app01.models.UserInfo ' >----> <app01.starkadmin.userconfig object at 0x00000072ddb6519 8> <class ' app01.models.Book ' >----> <stark.service.stark.modelstark object at 0x00000072ddb6524 0>"""            #distribute additions and deletions and change the searchTemp.append (URL (r'^%s/%s/'%(App_label, Model_name), STARK_CLASS_OBJ.URLS2)) """path (' app01/userinfo/', Userconfig (userinfo,site). urls2), path (' app01/book/', Modelstark (book,sit e). urls2),"""        returnTemp @propertydefURLs (self):returnself.get_urls (), None, Nonesite= Starksite ()

7 Stark configuration, generate 2-tier URLs

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.