Problem with get_absolute_url() in admin

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with get_absolute_url() in admin

Joakim Hove-4
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):
     return "http://127.0.0.1:8000/transaction/view{}/".format(self.id)

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

I am on Django 1.10


Regard Joakim


--
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 [hidden email].
To post to this group, send email to [hidden email].
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/c6e8b55d-56b5-40f4-8505-76f1447c1542%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with get_absolute_url() in admin

Gerald Brown-2
 In version 2 of Django that function has been removed.

For v 1.10 here is a link that might help: https://docs.djangoproject.com/en/1.10/ref/models/instances/#get-absolute-url

Here is another link that might be of help: https://stackoverflow.com/questions/13503645/what-is-permalink-and-get-absolute-url-in-django?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

Hope this helps.

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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with get_absolute_url() in admin

Joakim Hove-4
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

On Sun, Jun 10, 2018 at 2:19 PM Gerald Brown <[hidden email]> wrote:
 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!!!

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/CALKD1M8HqPs9rijWh63bAThh-o-6EsLNYFWLGQBm4%2BXjqkbB2g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with get_absolute_url() in admin

Melvyn Sopacua
In reply to this post by Joakim Hove-4

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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/2721239.SktWtCEFV2%40fritzbook.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with get_absolute_url() in admin

Anthony Anonde
In reply to this post by Joakim Hove-4
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



On Sunday, June 10, 2018 at 12:30:51 PM UTC+1, Joakim Hove wrote:
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( <a href="http://self.id" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fself.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGCR5B3zYr6QobAIs1Kda1VkZ0s9w&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fself.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGCR5B3zYr6QobAIs1Kda1VkZ0s9w&#39;;return true;">self.id )

things do not work from the Admin. When I click on the "View on Site" link i am redirected to "<a href="http://example.com/transaction/view/23635/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fexample.com%2Ftransaction%2Fview%2F23635%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH-f7RZWiQgoophLm1a2gmYKk2CWw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fexample.com%2Ftransaction%2Fview%2F23635%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH-f7RZWiQgoophLm1a2gmYKk2CWw&#39;;return true;">http://example.com/transaction/view/23635/" and I get an error message about failed XML parsing. I have no clue where the "<a href="http://example.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fexample.com\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH5iRlnhCg_mk89zURn-g01qQYJEw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fexample.com\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH5iRlnhCg_mk89zURn-g01qQYJEw&#39;;return true;">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):
     return "<a href="http://127.0.0.1:8000/transaction/view%7B%7D/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2F127.0.0.1%3A8000%2Ftransaction%2Fview%257B%257D%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFQlMS9Xzt66U-LtWLEpUbbrx0-dQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2F127.0.0.1%3A8000%2Ftransaction%2Fview%257B%257D%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFQlMS9Xzt66U-LtWLEpUbbrx0-dQ&#39;;return true;">http://127.0.0.1:8000/transaction/view{}/".format(<a href="http://self.id" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fself.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGCR5B3zYr6QobAIs1Kda1VkZ0s9w&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fself.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGCR5B3zYr6QobAIs1Kda1VkZ0s9w&#39;;return true;">self.id)

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

I am on Django 1.10


Regard Joakim


--
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 [hidden email].
To post to this group, send email to [hidden email].
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/5678b297-2693-4ae8-b249-08e42713ebf7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with get_absolute_url() in admin

Joakim Hove-4
In reply to this post by Melvyn Sopacua
 

> It is the default hostname for <a href="https://github.com/django/django/blob/master/django/contrib/sites/management.py#L28" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fblob%2Fmaster%2Fdjango%2Fcontrib%2Fsites%2Fmanagement.py%23L28\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEqL2X91ZQ-anBmWCkhZG6ZfheOXg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fblob%2Fmaster%2Fdjango%2Fcontrib%2Fsites%2Fmanagement.py%23L28\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEqL2X91ZQ-anBmWCkhZG6ZfheOXg&#39;;return true;">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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/dee9819f-fb1d-4a2f-9449-89ec0b3a8370%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with get_absolute_url() in admin

Melvyn Sopacua
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/1591437.ilrU06Q2TX%40fritzbook.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with get_absolute_url() in admin

Joakim Hove-4
Thank you;

that was indeed it!

On Sunday, June 10, 2018 at 11:03:54 PM UTC+2, Melvyn Sopacua wrote:
On zondag 10 juni 2018 22:54:10 CEST Joakim Hove wrote:
> > > It is the default hostname for django.contrib.sites
> >
> > <<a href="https://github.com/django/django/blob/master/django/contrib/sites/managem" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fblob%2Fmaster%2Fdjango%2Fcontrib%2Fsites%2Fmanagem\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF1timKDCJW9fbktrONlefG27l7rw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fblob%2Fmaster%2Fdjango%2Fcontrib%2Fsites%2Fmanagem\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF1timKDCJW9fbktrONlefG27l7rw&#39;;return true;">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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/af654af4-ff87-45bd-9da4-038cbefc09cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with get_absolute_url() in admin

Joakim Hove-4
In reply to this post by Anthony Anonde
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.

On Mon, Jun 11, 2018 at 12:58 AM Anthony Anonde <[hidden email]> wrote:
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



On Sunday, June 10, 2018 at 12:30:51 PM UTC+1, Joakim Hove wrote:
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


--
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 [hidden email].
To post to this group, send email to [hidden email].
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/5678b297-2693-4ae8-b249-08e42713ebf7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/CALKD1M_diEj6K9Mw7kPAvrjWs6JUjE1H0-zgB-0M8VFKSDVq%3DQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.