如何获取 current_app 以便在多部署可重用 Django 应用程序中与反向一起使用?
我正在编写可重用的应用程序。我想部署它几次。
这是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道这是一个相当老的问题...但我想我找到了一个解决方案:
正如 Will Hardy 建议的那样,您必须使两个实例的
app_name
保持相同(或者根本不定义它,它将默认为包含的网址所在的应用程序)。不过,为每个应用程序实例定义一个单独的命名空间:现在是在视图中设置当前活动的应用程序实例(命名空间)的稍微棘手的部分。这意味着您必须找出哪个应用程序实例处于活动状态并将其传递给
RequestContext
。要查找当前活动的应用程序,可以使用 django.urls.resolve:
因此,您必须相应地更新视图(假设使用基于类的视图):
现在 url 标记将自动反转到正确的命名空间,并且仍然允许在需要时反转到特定的应用程序命名空间:
要反转视图中的 url,必须手动传入当前应用程序实例:
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: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:So you'll have to update your views (this assumes using the class based views) accordingly:
Now the url tag will automatically reverse to the correct namespace and still allow reversing to a specific app namespace if needed:
To reverse urls in views, the current app instance has to be passed in manually:
在您的网址中,即使是同一个应用程序,您也有不同的
app_name
。将app_name
设置为相同的内容,并为每个实例设置唯一的namespace
。例如。然后在使用反向时提供
current_app
参数。请参阅 http://docs.djangoproject.com/en/dev/ topic/http/urls/#reverse 和 http://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 theapp_name
to the same thing, and setnamespace
uniquely for each instance. eg.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 calledcurrent_app
, which is automatically set based on the matched url.在探索这个主题几天后,我发现多次挂载 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.
我相信这就是
网站
框架是为了.I believe that's what the
sites
framework is for.