H
H
homm2012-01-08 22:50:00
Django
homm, 2012-01-08 22:50:00

Django - url generation issue?

The project has a bunch of templates, all URLs are built using the standard {% url %} tag. Some of the urls, say, look like this:

urlpatterns = patterns("",<br>
    (r'^factory/(?P<fac>\d+)/', include(patterns('',<br>
        (r'^$', factory.index),<br>
        (r'^assembly/(?P<asm>\d+)/$', factory.assembly),<br>
        (r'^division/(?P<div>\d+)/$', factory.division),<br>
        (r'^division/(?P<div>\d+)/assembly/(?P<asm>\d+)/$', factory.assembly),<br>
    ))),<br>
)

Those. factory is determined from the url. Some factories need separate subdomains with roughly the same pages and the same templates. Now it’s done stupidly - it redirects from the main subdomain to the attached factory. Those. factory is located in two parts of the url - in the domain and path info: super-factory.site.com/factory/25
I would like to make a separate urlconf for subdomains, in which to remove factory/(?P\d+)/ from the urls:
urlpatterns = patterns("",<br>
    (r'^', include(patterns('',<br>
        ……<br>
    )), {'fac': 'from_subdomain'}),<br>
)

The problem arises with the formation of urls. If you leave everything as it is:
{% url 'factory.assembly' fac=factory.id asm=assembly.id %}
then janga cannot find the required route. Wrapping each call in a separate {% if %} is the last thing. Tried making the factory part optional:
urlpatterns = patterns("",<br>
    (r'^(?:factory/(?P<fac>\d+)/)?', include(patterns('',<br>
        ……<br>
    )), {'fac': 'from_subdomain'}),<br>
)

In this case, the junga route finds, but does everything “correctly”, i.e. the factory/(?P\d+)/ part appears in the URL, which is almost the same as a redirect.
Another ugly option that comes to mind is a custom tag based on {% url %}, but checking some variable and excluding fac from the parameters. It's almost the same {% if %}, just outside the brackets.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Prophet, 2012-01-08
@Prophet

django-hosts should at first sight be suitable for this task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question