Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reverse and namespaced urls #1669

Closed
mronoffon opened this issue Mar 22, 2013 · 10 comments
Closed

reverse and namespaced urls #1669

mronoffon opened this issue Mar 22, 2013 · 10 comments

Comments

@mronoffon
Copy link

hello,

is there a fix for the reverse function? i'm using the latest cms, without any need for i18n

i'm trying to app_hook a 3rd party app that does not use 'urls.py'. so, i used the 3rdparty app's 'get_urls' function and passed that to the app_hook's 'urls' attr., and all is fine as i can see the urls. however, i can't seem to get the reverse to work with 'namespaced urls' in either the template or the code!

thanks in advance

@stefanfoulis
Copy link
Contributor

are you using the latest release (2.3.5) or the latest develop version from github?

in 2.3.5: namespaced urls don't work
in develop: I think this is fixed (but so far it is still not possible to add the apphook multiple times, see #1502 )

@mronoffon
Copy link
Author

i'm using the latest release. can you explain the part about 'adding apphook multiple times"? i currently have 3 hooks.

@stefanfoulis
Copy link
Contributor

This refers to using the same apphook multiple times in different locations. The problem is here, that all the views need to be aware of this and set the application namespace correctly. This is in order that url reversal (in templates and views) stay within the same instance of the app. Without namespacing it still works, but all the reverse urls will point to whatever urls where loaded first (or last, I'm not sure).

If you are using 3 different apphooks, or the same one but each in different languages that problem does not apply.

However, to get back to what I think your question was, django-cms 2.3.5 does not support connecting any urls that use namespaces with an apphook. There is a bug that eats the namespace and thus the reversing does not work anymore. sorry :-(

@mronoffon
Copy link
Author

well, here's the thing...when i remove the xxx from 'xxx:path', it appears to work, but then, there's no path 'info' as defined in the django.core.urlresolvers.py. so, when i click on a link, nothing happens because cms(?) doesn't know
'where' the obj/namespace is.

are there plans on fixing the use of reverse, what about writing some clever middleware?

@stefanfoulis
Copy link
Contributor

plans... yes :-)
I'm not sure on how to solve it yet and have not had the time to look into it in depth. Ideas on how to implement this are very welcome.

@mronoffon
Copy link
Author

would you mind clarifying this: "This refers to using the same apphook multiple times in different locations"?
also, all my views are class based with 'name=???' as an attr. any thought on reverse functionality?

thanks for your prompt response!!!

@stefanfoulis
Copy link
Contributor

"This refers to using the same apphook multiple times in different locations"

example: There is a blog app. Now you want to use it on the same site in multiple locations.
e.g /my/blog1/ and /my/blog2/. Whenever you browse within blog1 all reverse urls should stay within blog1 and the same for blog2. Django solves this with the app namespace and instance namespace (see django docs ).

Each of the two blogs would have its own instance namespace but they share the same app namespace. Lets assume the app namespace is blog and the instance namespaces are blog1 and blog2.
So in templates {% url 'blog:archive' %} would resolve to the archive url of whatever instance is currently active. {% url 'blog1:archive' %} would always resolve to blog1.
For the url templatetag to know what the current instance namespace is, the view must pass the app_namespace parameter to the RequestContext when rendering the response (see this stack overflow answer ) on how to do that. Additionally, whenever reverse is used in python code, the current_app parameter needs to be passed in with the current instance namespace.

In regular django the urls would be defined like this:

urlpatterns = patterns('',
    url(r'^my/blog1/', include('blog.urls', app_name='blog', namespace='blog1'),
    url(r'^my/blog2/', include('blog.urls', app_name='blog', namespace='blog2'),
)

But with AppHooks the namespace would need to be defined in the database and passed in with the cms generated url resolver.

also, all my views are class based with 'name=???' as an attr. any thought on reverse functionality?

I guess a view that supports namesspaces could look something like this (untested!):

from django.core.urlresolvers import resolve


class MyView(View):
    namespace = None

    def dispatch(self, request, *args, **kwargs):
        self.namespace = resolve(request.path).namespace
        return super(MyView, self).dispatch(request, *args, **kwargs)

    def get_success_url(self):
        return reverse('blog:archive', current_app=self.namespace)

    def render_to_response(self, context, **response_kwargs):
        response_kwargs['current_app'] = self.namespace
        return super(MyView, self).render_to_response(self, context, **response_kwargs)

Sorry for going off on a tangent here. I hope I'm at least remotely talking about what you wanted to know. ;-)

Actually I think it's pretty clear how to solve it now. Somebody (possibly me) just has to find the time to implement it.

@mronoffon
Copy link
Author

your 'tangent' help make a few things clear. thanks you!!!

@mronoffon
Copy link
Author

the code you have there mimics my thoughts. the problem is, i would have to rewrite all of the views of the 3rd party app to make it work.... something i'm trying to avoid.

'in develop: I think this is fixed (....'

so, basically, i could use the develop version of cms to solve my reverse('xxxx:yyyy') problem? i'm not so worried about multiple instances because there's only one cms app on one website. i'm merely trying to embed the 3rd party
app into cms. what do you think? how 'stable' is the develop vers, or even version 3?

@digi604
Copy link
Contributor

digi604 commented Jun 20, 2013

this is fixed in develop

@digi604 digi604 closed this as completed Jun 20, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants