引用模板中的数据浏览 URL

发布于 2024-11-06 12:41:40 字数 212 浏览 0 评论 0原文

我想将 django Databrowse 集成到我的应用程序中。

它归结为从模板或视图中指向数据浏览 URL,以增强数据浏览的向下钻取功能。

有没有一种简单的方法可以从数据浏览对象中提取 url?

I'd like to integrate django Databrowse into my application.

It comes down to pointing to databrowse urls from within template or view for enhanced drill down functionality of the databrowse.

Is there an easy way to extract the url from a databrowse object?

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

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

发布评论

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

评论(3

深府石板幽径 2024-11-13 12:41:40

好吧,一种简单的方法是构造您想要的 url,并将其传递到模板中:

databrowse_url = '/'.join((obj._meta.app_label, obj._meta.module_name, 'objects', str(obj.id)))

然后在模板中(假设您的 databrowse 位于 /databrowse

<a href="/databrowse/{{ databrowse_url }}">

这将为您提供一个 url例如:/databrowse/app_name/model_name/objects/1

Well, one easy way would be to just construct the url you want, and pass it into the template:

databrowse_url = '/'.join((obj._meta.app_label, obj._meta.module_name, 'objects', str(obj.id)))

And then in the template (assuming you have databrowse situated at /databrowse:

<a href="/databrowse/{{ databrowse_url }}">

Which would give you a url like: /databrowse/app_name/model_name/objects/1.

随波逐流 2024-11-13 12:41:40

您可以按照 中显示的格式重新创建数据浏览 URL databrowse urls.py

您也许可以获得 url标记 通过传递视图名称+参数来在模板中工作。

但是,如果您浏览源,它看起来 databrowse 会向它使用的对象添加一个“url”属性。

编辑:

给定一个 EasyModel 实例,您可以执行以下操作:

my_easy_model_instance.url()

大多数“Easy”类都有 url() 或 urls() 方法。

You could recreate the databrowse urls in the format thats show in the databrowse urls.py

You might be able to get the url tag to work in your template by passing the view name + arguments.

However, if you browse the source, it looks like databrowse will add a 'url' attribute to the objects it works with.

EDIT:

Given an EasyModel instance, you can do the following:

my_easy_model_instance.url()

Most of the 'Easy' classes have a url() or urls() method on them.

浅语花开 2024-11-13 12:41:40

最终编写了 mixin 类来获取相关的 EasyInstance 并重用它的 url() :

from django.contrib.databrowse.datastructures import EasyModel
from django.contrib.databrowse import site   

class DatabrowseMixin:
     def url(pyClass):
          if not site.root_url:
               #hack, but root_url is not set until the first databrowse view
               #and resolving urlconf does not work either
               site.root_url = '/databrowse/' 

          easy_model = EasyModel(site, pyClass.__class__)
          obj = easy_model.object_by_pk(pyClass.pk)

          return obj.url()

class MyModel(models.Model, DatabrowseMixin):
     ...

现在在我的模板中,我可以重用指向数据浏览对象 url 的 my_model_instance.url 标签。

Ended up writing mixin class that fetches relevant EasyInstance and reuses the url() of it:

from django.contrib.databrowse.datastructures import EasyModel
from django.contrib.databrowse import site   

class DatabrowseMixin:
     def url(pyClass):
          if not site.root_url:
               #hack, but root_url is not set until the first databrowse view
               #and resolving urlconf does not work either
               site.root_url = '/databrowse/' 

          easy_model = EasyModel(site, pyClass.__class__)
          obj = easy_model.object_by_pk(pyClass.pk)

          return obj.url()

class MyModel(models.Model, DatabrowseMixin):
     ...

Now in my templates I can reuse my_model_instance.url tag pointing to databrowse object url.

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