0

I need 2 URLs to show the exact same view and URL patterns, so in my main urls.py file, I do this:

url(r'^evenements/', include('project.events.urls', namespace='events')),
url(r'^tourisme/evenements/', include('project.events.urls', namespace="tourisme_event", app_name='events')),

And then in my events app, I have this in the urls.py

urlpatterns = patterns('',
    url(r'^$', views.listing, name='index'),
    url(r'^(?P<slug>.*)/$', views.detail, name='detail'),
)

Now what I'm trying to achieve is to show the exact same view for both the URLs, but I need the links to work with this in the template:

{% url 'events:detail' event.event.slug %}

To my understanding, using "app_name" should allow me to do just that, but here are the different things that happen:

  • app_name only on "tourisme/evenements": both pages show the "tourisme/evenements" URL with the {% url %} tag
  • app_name on both: both pages show the "evenements" URL with the {% url %} tag
  • app_name on neither: both pages show the "evenements" URL with the {% url %} tag

I can't get it to show a different URL on both pages. I did what was told in this answer: https://stackoverflow.com/a/8039846/2174532

Any idea why that wouldn't work? BTW, I'm on Django 1.6.

Thanks

1 Answer 1

1

Since you're not on 1.9 yet, you need to manually set a namespace hint to the current instance namespace. You can do so by passing the current_app to render() (or directly to your context instance):

render(request, 'template.html', current_app=request.resolver_match.namespace)

In 1.8 you set request.current_app instead of passing the namespace to the context. In 1.9, this is done for you.

You need to set the app_name to 'events' in both includes. Django will look for an application namespace, and only fall back to an instance namespace if no application namespace is found. If you name an instance namespace with no app_name the same as an application namespace, you'll never be able to reverse it.

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

Comments

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.