开发者

Referring to databrowse urls in templates

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?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜