0

I want to make an include in my urls.py referring to another urls in the same urls.py file.

My structure is as follows:

├── docs
├── requirements
├── scripts
└── sonata
    ├── person
    │   ├── migrations
    │   ├── templatetags
    │   └── urls.py
    ├── registration
    │   ├── migrations
    │   └── urls.py
    └── sonata
        ├── settings
        └── urls.py

And I want every coming url with prefix 'pdf/' to add a value to kwargs and calling again the rest of url. This is my attempt:

urlpatterns = patterns('',
    url(r'^$',TemplateView.as_view(template_name='registration/login.html')),

    # This is my attempt for capturing the pdf prefix 
    # and calling this same file afterwards with the pdfOutput variable set to True
    url(r'^pdf/', include('sonata.urls'), kwargs={'pdfOutput':True}),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^person/', include('person.urls')),
    url(r'^registration/', include('registration.urls')),
    url(r'^menu/', registration_views.menu, name='menu'),
    url(r'^customers/', person_views.customers, name='customers'),
    url(r'^employees/', person_views.employees, name='employees'),
    url(r'^alumns/', person_views.alumns, name='alumns'),
    url(r'^teaching/', person_views.docencia, name='teaching'),
    url(r'^i18n/', include('django.conf.urls.i18n')),
)

Is there any way of doing this? I looked at the docs. It seems pretty clear how to pass values, but after proving I can't make the include. (I don't want either to do include() with the [array] of patterns repeating all urls. This would break DRY principle).

Thanks in advance

1 Answer 1

2

The problem is that include immediately imports the included url configuration, resulting in a circular import error. You can do something like this, though:

sonata_patterns = [
    url(r'^$',TemplateView.as_view(template_name='registration/login.html')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^person/', include('person.urls')),
    url(r'^registration/', include('registration.urls')),
    url(r'^menu/', registration_views.menu, name='menu'),
    url(r'^customers/', person_views.customers, name='customers'),
    url(r'^employees/', person_views.employees, name='employees'),
    url(r'^alumns/', person_views.alumns, name='alumns'),
    url(r'^teaching/', person_views.docencia, name='teaching'),
    url(r'^i18n/', include('django.conf.urls.i18n')),
]

urlpatterns = [
    url(r'^pdf/', include(sonata_patterns), kwargs={'pdfOutput':True})
] + sonata_patterns

Another possibility is to use middleware to capture the pdf prefix and set an attribute on the request. That way, you won't have to worry if all views accept your pdfOutput argument.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. It's too late here now. I will test this soon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.