如何获取 current_app 以便在多部署可重用 Django 应用程序中与反向一起使用?

发布于 2024-08-17 14:40:17 字数 643 浏览 3 评论 0原文

我正在编写可重用的应用程序。我想部署它几次。

这是 urls.py:

urlpatterns = patterns('',
(r'^carphotos/', include('webui.photos.urls', app_name='car-photos') ),
(r'^userphotos/', include('webui.photos.urls',  app_name='profile-photos') ),)

和 photos/urls.py:

urlpatterns = patterns('webui.photos.views',
url(r'^$', album_list, name="album-list" )
url(r'^newalbum/$', album_page, {'create': True}, name="album-create"),)

在 album_list 视图上,我想显示用于创建新相册 album_page 的 url。

我发现我必须使用反向函数的参数 current_app 才能获取正确的 URL。

但是如何获取这个current_app呢? 我认为答案很简单。但我在 django 文档中找不到它。

谢谢,尼克

I'm writing reusable app. And I want to deploy it several times.

Here is urls.py:

urlpatterns = patterns('',
(r'^carphotos/', include('webui.photos.urls', app_name='car-photos') ),
(r'^userphotos/', include('webui.photos.urls',  app_name='profile-photos') ),)

and photos/urls.py:

urlpatterns = patterns('webui.photos.views',
url(r'^

On the album_list view I want to show url for creating new album album_page.

I found that I have to use parameter current_app of reverse function to get proper URL.

But how to get this current_app?
I thought the answer is something simple. But I can't find it in django documentation.

Thanks, Nick

, album_list, name="album-list" ) url(r'^newalbum/

On the album_list view I want to show url for creating new album album_page.

I found that I have to use parameter current_app of reverse function to get proper URL.

But how to get this current_app?
I thought the answer is something simple. But I can't find it in django documentation.

Thanks, Nick

, album_page, {'create': True}, name="album-create"),)

On the album_list view I want to show url for creating new album album_page.

I found that I have to use parameter current_app of reverse function to get proper URL.

But how to get this current_app?
I thought the answer is something simple. But I can't find it in django documentation.

Thanks, Nick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

可遇━不可求 2024-08-24 14:40:17

我知道这是一个相当老的问题...但我想我找到了一个解决方案:

正如 Will Hardy 建议的那样,您必须使两个实例的 app_name 保持相同(或者根本不定义它,它将默认为包含的网址所在的应用程序)。不过,为每个应用程序实例定义一个单独的命名空间:

urlpatterns = patterns('',
    (r'^carphotos/', include('webui.photos.urls', app_name="webui_photos", namespace='car-photos') ),
    (r'^userphotos/', include('webui.photos.urls', app_name="webui_photos", namespace='profile-photos') ),
)

现在是在视图中设置当前活动的应用程序实例(命名空间)的稍微棘手的部分。这意味着您必须找出哪个应用程序实例处于活动状态并将其传递给 RequestContext

要查找当前活动的应用程序,可以使用 django.urls.resolve:

r = resolve(request.path)
r.app_name  # the app name
r.namespace # the the currently active instance

因此,您必须相应地更新视图(假设使用基于类的视图):

from django.urls import resolve
from django.views.generic import TemplateView


class AlbumCreateView(TemplateView):
    template_name = 'path/to/my/template.html'
    
    def render_to_response(self, context, **response_kwargs):
        response_kwargs['current_app'] = resolve(self.request.path).namespace
        return super(AlbumPageView, self).render_to_response(context, **response_kwargs)

现在 url 标记将自动反转到正确的命名空间,并且仍然允许在需要时反转到特定的应用程序命名空间:

{% url webui_photos:album-create %} {# returns the url for whatever app is current #}
{% url car-photos:album-create %}
{% url profile-photos:album-create %}

要反转视图中的 url,必须手动传入当前应用程序实例:

reverse('webui_photos:album-create', current_app=resolve(self.request.path).namespace))

I know this is a pretty old question... but I think I found a solution:

As Will Hardy suggested you'll have to keep app_name the same for both instances (or not define it at all, it will default to the app the included urls reside in). Define a separate namespace for each app instance though:

urlpatterns = patterns('',
    (r'^carphotos/', include('webui.photos.urls', app_name="webui_photos", namespace='car-photos') ),
    (r'^userphotos/', include('webui.photos.urls', app_name="webui_photos", namespace='profile-photos') ),
)

Now comes the slightly tricky part of setting the currently active app instance (namespace) in your views. Meaning you have to find out which app instance is active and pass it to RequestContext.

To find the currently active app, django.urls.resolve can be used:

r = resolve(request.path)
r.app_name  # the app name
r.namespace # the the currently active instance

So you'll have to update your views (this assumes using the class based views) accordingly:

from django.urls import resolve
from django.views.generic import TemplateView


class AlbumCreateView(TemplateView):
    template_name = 'path/to/my/template.html'
    
    def render_to_response(self, context, **response_kwargs):
        response_kwargs['current_app'] = resolve(self.request.path).namespace
        return super(AlbumPageView, self).render_to_response(context, **response_kwargs)

Now the url tag will automatically reverse to the correct namespace and still allow reversing to a specific app namespace if needed:

{% url webui_photos:album-create %} {# returns the url for whatever app is current #}
{% url car-photos:album-create %}
{% url profile-photos:album-create %}

To reverse urls in views, the current app instance has to be passed in manually:

reverse('webui_photos:album-create', current_app=resolve(self.request.path).namespace))
被你宠の有点坏 2024-08-24 14:40:17

在您的网址中,即使是同一个应用程序,您也有不同的 app_name。将 app_name 设置为相同的内容,并为每个实例设置唯一的 namespace。例如。

urlpatterns = patterns('',
(r'^carphotos/', include('webui.photos.urls', app_name="webui_photos", namespace='car-photos') ),
(r'^userphotos/', include('webui.photos.urls', app_name="webui_photos", namespace='profile-photos') ),)

然后在使用反向时提供 current_app 参数。请参阅 http://docs.djangoproject.com/en/dev/ topic/http/urls/#reversehttp://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces

[编辑]重新阅读您的问题后:

您不需要提供 current_app 参数(如果您使用的是 {% url %} 标记)。据我所知,它将自动访问一个名为 current_app 的模板变量,该变量是根据匹配的 url 自动设置的。

In your urls, you have a different app_name even though it's the same app. Set the app_name to the same thing, and set namespace uniquely for each instance. eg.

urlpatterns = patterns('',
(r'^carphotos/', include('webui.photos.urls', app_name="webui_photos", namespace='car-photos') ),
(r'^userphotos/', include('webui.photos.urls', app_name="webui_photos", namespace='profile-photos') ),)

Then provide the current_app argument when using reverse. See http://docs.djangoproject.com/en/dev/topics/http/urls/#reverse and http://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces

[edit] after re-reading your question:

you don't need to provide the current_app argument if you are using the {% url %} tag. As far as I'm lead to believe, it will automatically access a template variable called current_app, which is automatically set based on the matched url.

美男兮 2024-08-24 14:40:17

在探索这个主题几天后,我发现多次挂载 django app 并不自然。

可插拔应用程序模式的实现:
http://github.com/nowells/django-pluggables
对我来说看起来太棘手了。

因此,我决定将重复的功能移至自定义标签,并为我的应用程序的每次使用复制模板。我希望使用自定义标签和扩展功能可以帮助我遵循 DRY 原则。

After exploring this topic for several days, I found that it isn't natural to mount django app more than once.

There is implementation of pluggable applications pattern:
http://github.com/nowells/django-pluggables.
It looks too tricky for me.

So I decided to move repeated functionality to custom tags and duplicate templates for each usage of my app. I hope using custom tags and extend feature help me to follow DRY principle.

因为看清所以看轻 2024-08-24 14:40:17

我相信这就是网站框架是为了.

I believe that's what the sites framework is for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文