Getting Django admin url for an object

前端 未结 9 1568
有刺的猬
有刺的猬 2020-12-02 03:51

Before Django 1.0 there was an easy way to get the admin url of an object, and I had written a small filter that I\'d use like this:

相关标签:
9条回答
  • 2020-12-02 04:12

    Essentially the same as Mike Ramirez's answer, but simpler and closer in stylistics to django standard get_absolute_url method:

    from django.urls import reverse
    
    def get_admin_url(self):
        return reverse('admin:%s_%s_change' % (self._meta.app_label, self._meta.model_name),
                       args=[self.id])
    
    0 讨论(0)
  • 2020-12-02 04:13

    I solved this by changing the expression to:

    reverse( 'django-admin', args=["%s/%s/%s/" % (app_label, model_name, object_id)] )
    

    This requires/assumes that the root url conf has a name for the "admin" url handler, mainly that name is "django-admin",

    i.e. in the root url conf:

    url(r'^admin/(.*)', admin.site.root, name='django-admin'),
    

    It seems to be working, but I'm not sure of its cleanness.

    0 讨论(0)
  • 2020-12-02 04:16

    For pre 1.1 django it is simple (for default admin site instance):

    reverse('admin_%s_%s_change' % (app_label, model_name), args=(object_id,))
    
    0 讨论(0)
  • 2020-12-02 04:24

    You can use the URL resolver directly in a template, there's no need to write your own filter. E.g.

    {% url 'admin:index' %}

    {% url 'admin:polls_choice_add' %}

    {% url 'admin:polls_choice_change' choice.id %}

    {% url 'admin:polls_choice_changelist' %}

    Ref: Documentation

    0 讨论(0)
  • 2020-12-02 04:25

    Here's another option, using models:

    Create a base model (or just add the admin_link method to a particular model)

    class CommonModel(models.Model):
        def admin_link(self):
            if self.pk:
                return mark_safe(u'<a target="_blank" href="../../../%s/%s/%s/">%s</a>' % (self._meta.app_label,
                        self._meta.object_name.lower(), self.pk, self))
            else:
                return mark_safe(u'')
        class Meta:
            abstract = True
    

    Inherit from that base model

       class User(CommonModel):
            username = models.CharField(max_length=765)
            password = models.CharField(max_length=192)
    

    Use it in a template

    {{ user.admin_link }}
    

    Or view

    user.admin_link()
    
    0 讨论(0)
  • 2020-12-02 04:27

    I had a similar issue where I would try to call reverse('admin_index') and was constantly getting django.core.urlresolvers.NoReverseMatch errors.

    Turns out I had the old format admin urls in my urls.py file.

    I had this in my urlpatterns:

    (r'^admin/(.*)', admin.site.root),
    

    which gets the admin screens working but is the deprecated way of doing it. I needed to change it to this:

    (r'^admin/', include(admin.site.urls) ),
    

    Once I did that, all the goodness that was promised in the Reversing Admin URLs docs started working.

    0 讨论(0)
提交回复
热议问题