Problem with get_absolute_url() in admin

34 views
Skip to first unread message

Joakim Hove

unread,
Jun 10, 2018, 7:30:51 AM6/10/18
to Django users
Hello,

I have a model `Transaction` which can be viewed at the url: /transaction/view/$ID/ - if I just enter that url in the browser, or in template everything works. But when I have the following get_abolute_url() method:

def get_absolute_url(self):
     return "/transaction/view/{}/".format( self.id )

things do not work from the Admin. When I click on the "View on Site" link i am redirected to "http://example.com/transaction/view/23635/" and I get an error message about failed XML parsing. I have no clue where the "example.com" address comes from - that string is not in my codebase. If I hardcode the get_absolute_url to:

def get_absolute_url(self):

That works in development, but needless to say that is not a very good approach.

I am on Django 1.10


Regard Joakim


Gerald Brown

unread,
Jun 10, 2018, 8:18:51 AM6/10/18
to Django users
 In version 2 of Django that function has been removed.



Hope this helps.

BTW the first link says V1.10 is obsolete and is no longer supported!!!

Joakim Hove

unread,
Jun 10, 2018, 8:34:20 AM6/10/18
to django...@googlegroups.com
Thank  you for answering;

but I do not agree fully with your answer:

1. It is the @permalink decorator which is removed from Django 2.x - not the get_abolute_url() method.
2. I agree using the reverse in the get_absolute_url() is nicer, and I have also tried that - but the ersults were the same ... ;-(
3. Yes - I should upgrade

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1c0b4b32-7154-4dd4-b437-92d0bba6c70e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Melvyn Sopacua

unread,
Jun 10, 2018, 4:51:39 PM6/10/18
to django...@googlegroups.com

On zondag 10 juni 2018 13:30:51 CEST Joakim Hove wrote:

 

> things do not work from the Admin. When I click on the "View on Site" link

> i am redirected to "http://example.com/transaction/view/23635/" and I get

> an error message about failed XML parsing. I have no clue where the

> "example.com" address comes from - that string is not in my codebase.

 

It is the default hostname for django.contrib.sites with SITE_ID = 1.

--

Melvyn Sopacua

Joakim Hove

unread,
Jun 10, 2018, 4:54:10 PM6/10/18
to Django users
 

> It is the default hostname for django.contrib.sites with SITE_ID = 1.



OK - so that might be a smoking gun that I have not configured the "sites" model correctly? I have so far not really related to the "sites" functionality at all.

Melvyn Sopacua

unread,
Jun 10, 2018, 5:03:54 PM6/10/18
to django...@googlegroups.com
On zondag 10 juni 2018 22:54:10 CEST Joakim Hove wrote:
> > > It is the default hostname for django.contrib.sites
> >
> > <https://github.com/django/django/blob/master/django/contrib/sites/managem
> > ent.py#L28> with SITE_ID = 1.
>
> OK - so that might be a smoking gun that I have not configured the "sites"
> model correctly? I have so far not really related to the "sites"
> functionality at all.

It's enabled by default. And from the admin you can edit that hostname and add
more sites.
Because Django supports multiple sites with a single admin, it generates fully
qualified URLs for the "view on site" functionality, if django.contrib.sites is
in INSTALLED_APPS.

--
Melvyn Sopacua

Joakim Hove

unread,
Jun 10, 2018, 5:23:55 PM6/10/18
to Django users
Thank you;

that was indeed it!

Anthony Anonde

unread,
Jun 10, 2018, 6:58:36 PM6/10/18
to Django users
the example.com is am example django give for Site. Which means there is a way for you to manage more than one site at a time, basically using the site_id.
To you Questions, i don't understand why you want to hard code your uri routing like that when you can easily use the app_name and name convention which is more dynamic then hard coding it like that: 
for example at the model Transaction urls.py 
app_name = "trans"
url(r'(?P<pk>-/d+)$', views.func, name='account_trans'),

def get_absolute_url(self):
    return reverse("trans:account_trans",  kwargs={"pk":self.pk})


and you will be just fine.... hope this help

Joakim Hove

unread,
Jun 11, 2018, 10:18:42 AM6/11/18
to django...@googlegroups.com
Thank you;

my main problem was with the sites model - and the default value for SITE_ID == 1. I had no intention of hardcoding the URL.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages