在 django 的模板上为我们获取当前使用的 url 面包屑?
我有一个应用程序有两个用途 - 显示我公司的成员和中心。它们的工作原理完全相同,只是在过滤模型时保存不同的变量。问题是我无法将当前网址获取到模板以在自定义面包屑中使用。
我的主 urls.py: 中有这个 urlpattern,
# --- urls.py ----
url(r'^find-member/', include('company.directory.urls'), \
{'which_app': 'members'}, name='find_member'),
url(r'^find-centre/', include('company.directory.urls'), \
{'which_app': 'training'}, name='find_centre'),
其中链接到我的应用程序 urls.py:
# ---- company/urls.py ----
from django.conf.urls.defaults import *
urlpatterns = patterns('company.directory.views',
url(r'^$', 'index'),
url(r'^(?P<slug>\w+)/$', 'index'),
)
在我的模板上,我希望创建一个指向第一个 urlpatten 的链接,以便与我的自定义面包屑一起使用
<a href='/find-member/'>members</a>
,或者
<a href='/find-centre/'>Centre</a>
基于我正在使用的 url应用程序与.
我的视图如下所示:
# ---- company/view.py ----
def index(request, which_app=None, slug=None):
#r = reverse('' ,kwargs={'which_app'=training )
s = "%s %s" % (which_app, slug)
return render_to_response('directory/index.html', locals())
我想根据传递到 def 的 which_app
变量找到 url。 我似乎无法使用 resolve()
或 reverse()
。我可能做错了。我现在还没有真正可以展示的模板。
有人有什么建议吗?我很想得到一些建议。
提前致谢。
I have an app which serves two purposes - displays members and centres of my company. They both work exactly the same, save a different variable when filtering my model. Problem is I can't get the current url onto the template to use in my custom breadcrumbs.
I have this urlpattern in my main urls.py:
# --- urls.py ----
url(r'^find-member/', include('company.directory.urls'), \
{'which_app': 'members'}, name='find_member'),
url(r'^find-centre/', include('company.directory.urls'), \
{'which_app': 'training'}, name='find_centre'),
of which links to my app urls.py:
# ---- company/urls.py ----
from django.conf.urls.defaults import *
urlpatterns = patterns('company.directory.views',
url(r'^
on my template I wish to create a link to the first urlpatten for use with my custom breadcrumbs
<a href='/find-member/'>members</a>
or
<a href='/find-centre/'>Centre</a>
based upon which url I'm using the app with.
my view looks like this:
# ---- company/view.py ----
def index(request, which_app=None, slug=None):
#r = reverse('' ,kwargs={'which_app'=training )
s = "%s %s" % (which_app, slug)
return render_to_response('directory/index.html', locals())
I would like to find the url based upon the which_app
variable passed into the def.
I can't seem to use resolve()
or reverse()
. I'm probably doing it wrong. I haven't really got a template to show right now.
Does anybody have any suggestions? I'd love some advice.
Thanks in advance.
, 'index'),
url(r'^(?P<slug>\w+)/
on my template I wish to create a link to the first urlpatten for use with my custom breadcrumbs
or
based upon which url I'm using the app with.
my view looks like this:
I would like to find the url based upon the which_app
variable passed into the def.
I can't seem to use resolve()
or reverse()
. I'm probably doing it wrong. I haven't really got a template to show right now.
Does anybody have any suggestions? I'd love some advice.
Thanks in advance.
, 'index'),
)
on my template I wish to create a link to the first urlpatten for use with my custom breadcrumbs
or
based upon which url I'm using the app with.
my view looks like this:
I would like to find the url based upon the which_app
variable passed into the def.
I can't seem to use resolve()
or reverse()
. I'm probably doing it wrong. I haven't really got a template to show right now.
Does anybody have any suggestions? I'd love some advice.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要使用函数。您的视图将传递
request
对象,该对象具有一个属性path
,它是被调用的路径。请参阅请求文档。You don't need to use a function. Your view is passed the
request
object, which has an attributepath
which is the path that was called. See the request docs.