在 django 的模板上为我们获取当前使用的 url 面包屑?

发布于 2024-10-12 14:37:09 字数 1350 浏览 2 评论 0原文

我有一个应用程序有两个用途 - 显示我公司的成员和中心。它们的工作原理完全相同,只是在过滤模型时保存不同的变量。问题是我无法将当前网址获取到模板以在自定义面包屑中使用。


我的主 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 技术交流群。

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

发布评论

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

评论(1

染年凉城似染瑾 2024-10-19 14:37:09

您不需要使用函数。您的视图将传递 request 对象,该对象具有一个属性 path ,它是被调用的路径。请参阅请求文档

You don't need to use a function. Your view is passed the request object, which has an attribute path which is the path that was called. See the request docs.

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