2012-10-02 24 views
11

मैं एक Django URLConf में एक नेस्टेड यूआरएल नेमस्पेस (जो look:like:this) को परिभाषित करने का तरीका जानने का प्रयास कर रहा हूं।किसी URLConf में नेस्टेड नेमस्पेस को परिभाषित करने के लिए, Django URL को उलट करने के लिए - क्या किसी के पास एक कॉगेंट उदाहरण है?

इस से पहले, मैं पता लगा कि कैसे एक बुनियादी यूआरएल नाम स्थान करने के लिए और this simple example snippet के साथ आया था, जिसमें आप एक urls.py फ़ाइल में डाल सकता है:

from django.conf.urls import patterns, include, url 

# you can only define a namespace for urls when calling include(): 

app_patterns = patterns('', 
    url(r'^(?P<pk>[\w\-]+)/$', 'yourapp.views.your_view_function', 
     name="your-view"), 
) 

urlpatterns = patterns('', 
    url(r'^view-function/', include(app_patterns, 
     namespace='yournamespace', app_name='yourapp')), 
) 

""" 

    You can now use the namespace when you refer to the view, e.g. a call 
    to `reverse()`: 

    # yourapp/models.py 

    from django.core.urlresolvers import reverse 

    # ... 

    class MyModel(models.Model): 

     def get_absolute_url(self): 
     return reverse('signalqueue:exception-log-entry', kwargs=dict(pk=self.pk)) 

""" 

... डब्ल्यू/आर/कटौती टी जिनमें से the Django documentation इस मामले में, सभी सहायक नहीं थे। जबकि Django का दस्तावेज़ अन्य सभी मामलों में शानदार है, और यह नियम के लिए अपवाद है, नेस्टेड यूआरएल नेमस्पेस को परिभाषित करने के बारे में भी कम जानकारी है।

इसके बजाय मेरी spaghettified प्रयास पोस्टिंग † इस यह पता लगाने की की

, मैंने सोचा कि मैं अगर किसी को भी नहीं है, या का पता एक URLconf कि एक नेस्टेड नाम स्थान को परिभाषित करता है की पूछ सकते हैं, एक सीधी अकाट्य और/या आत्म व्याख्यात्मक उदाहरण यह है कि वे साझा कर सकता है

विशेष रूप से मैं दृश्यों को उपसर्ग करने वाले घोंसले वाले हिस्सों के बारे में उत्सुक हूं: सभी को Django ऐप्स इंस्टॉल किया जाना चाहिए?

†) उत्सुकता के लिए, यहां एक (शायद कुछ हद तक अचूक) उदाहरण है: http://imgur.com/NDn9H। मैं testapp:<viewname> के बजाय testapp:views:<viewname> नामित करने के लिए नीचे लाल और हरे रंग में यूआरएल मुद्रित करने की कोशिश कर रहा था।

उत्तर

21

यह बल्कि सहज काम करता है। include एक urlconf जिसमें अभी तक एक और नामित include का नाम घोंसला वाले नामस्थान होगा।

## urls.py 
nested2 = patterns('', 
    url(r'^index/$', 'index', name='index'), 
) 

nested1 = patterns('', 
    url(r'^nested2/', include(nested2, namespace="nested2"), 
    url(r'^index/$', 'index', name='index'), 
) 

urlpatterns = patterns('', 
    (r'^nested1/', include(nested1, namespace="nested1"), 
) 

reverse('nested1:nested2:index') # should output /nested1/nested2/index/ 
reverse('nested1:index') # should output /nested1/index/ 

यह यूआरएल व्यवस्थित रखने का एक शानदार तरीका है। मुझे लगता है कि सबसे अच्छा सलाह मैं दे सकता है कि include एक patterns वस्तु सीधे ले जा सकते हैं (मेरे उदाहरण के रूप में) याद करने के लिए जो आप एक से अधिक यूआरएल फ़ाइलों को बनाने के बिना उपयोगी नामस्थान में एक भी urls.py और विभाजन विचारों का उपयोग करने देता है।

+0

अच्छा, कि वास्तव में सरल है। धन्यवाद! – fish2000

4

जबकि युजी के जवाब सही है, ध्यान दें कि django.conf.urls.patterns नहीं रह गया है (Django 1.10 के बाद से) मौजूद है और सादे सूचियों बजाय प्रयोग किया जाता है।

एक ही उदाहरण urls.py अब इस तरह होना चाहिए:

from django.conf.urls import include, url 

nested2 = [ 
    url(r'^index/$', 'index', name='index'), 
] 

nested1 = [ 
    url(r'^nested2/', include(nested2, namespace="nested2"), 
    url(r'^index/$', 'index', name='index'), 
] 

urlpatterns = [ 
    url(r'^nested1/', include(nested1, namespace="nested1"), 
] 

और फिर भी तरह इस्तेमाल किया:

reverse('nested1:nested2:index') # should output /nested1/nested2/index/ 
reverse('nested1:index') # should output /nested1/index/ 
+0

'urlpatterns' चर भी' url' समारोह फोन है नहीं करना चाहिए? –

+0

@NathanJones उफ़ हाँ, धन्यवाद - ठीक कर दिया। –