Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#27757 closed Bug (invalid)

The link of the "view_on_site" button is not formed according to the returning result of the "get_absolute_url" method.

Reported by: George Tantiras Owned by: nobody
Component: contrib.admin Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by George Tantiras)

I am using Django-1.10.5 under Python-3.4.2.

from django.db import models


class Product(models.Model):
    name = models.CharField(verbose_name="Name", max_length=100, unique=True)

    def get_absolute_url(self):
        return "test-string/{0}".format(self.id)


class Flavor(models.Model):
    product = models.ForeignKey(Product, verbose_name="Product")
    name = models.CharField(verbose_name="Name", max_length=100)


    def get_absolute_url(self):
        return "test-string-2/{0}".format(self.id)

admin.py:

from django.contrib import admin
from inline_online.models import Flavor, Product


class FlavorInline(admin.TabularInline):
    model = Flavor


class ProductAdmin(admin.ModelAdmin):
    list_display = ("id", "name")
    inlines = [FlavorInline, ]


admin.site.register(Product, ProductAdmin)


class FlavorAdmin(admin.ModelAdmin):
    view_on_site = True


admin.site.register(Flavor, FlavorAdmin)

The view_on_site link appears either in the main model admin view or in the inlines but the url is not the one returned from get_absolute_url method (example: /admin/r/10/1)

A very old ticket seem to reference the same issue: https://code.djangoproject.com/ticket/7984

Change History (11)

comment:1 by George Tantiras, 8 years ago

Summary: vIew_on_site is not working with inline modelsview_on_site is not working with inline models

comment:2 by George Tantiras, 8 years ago

Description: modified (diff)
Summary: view_on_site is not working with inline modelsview_on_site does not receive the get_absolute_url returning uri

comment:3 by George Tantiras, 8 years ago

Description: modified (diff)

comment:4 by George Tantiras, 8 years ago

Description: modified (diff)
Summary: view_on_site does not receive the get_absolute_url returning uriThe link of the "view_on_site" button is not formed according to the returning result of the "get_absolute_url" method.

comment:5 by Tim Graham, 8 years ago

Component: Uncategorizedcontrib.admin
Resolution: invalid
Status: newclosed
Type: UncategorizedBug

The view on site links will redirect from something like /admin/r/62/1/ to the contents of get_absolute_url(), however, it looks like you made a mistake: get_absolute_url() must return a value that starts with a slash.

comment:6 by George Tantiras, 8 years ago

I apologize for this mistake.

However, in the relevant django docs reverse url returns a string which begins with 'https://'. Correcting my get_absolute_url(), to return a string which begins either with 'https://' or with a slash (/xxx) the view on site link will just return its standard /admin/r/62/1/.

On the other hand when I use an external admin app like django-suit, while the main link works as expected (it receives the get_absolute_url() value), the inline link -if it appears- (I cannot figure out under which circumstances it will appear) it redirects to the wrong /admin/r/62/1/.

I will verify all the above in my main project where I use reverse() in get_absolute_url().

Last edited 8 years ago by George Tantiras (previous) (diff)

comment:7 by Tim Graham, 8 years ago

Does the /admin/r/62/1/ URL redirect to the wrong get_absolute_url()? Maybe I'm misunderstanding but your complaint seems to be that the /admin/r/62/1/ URL appears in the first place (i.e. you don't want to an intermediate redirect), but as far as I know, this is expected behavior.

comment:8 by George Tantiras, 8 years ago

For example:

class MyModel(models.Model):
...

    def get_absolute_url(self, obj):
        return 'http://127.0.0.1:8000/test'

...

Using native django admin, view_on_site returns: http://127.0.0.1:8000/admin/r/19/3305/
Enabling django-suit, view_on_site returns: http://127.0.0.1:8000/test

The relevant links when the model is an inline, are a pure mess do not seem to work as expected.

Last edited 8 years ago by George Tantiras (previous) (diff)

comment:9 by Tim Graham, 8 years ago

I see URLs like /admin/r/62/1/ redirecting to URLsfrom get_absolute_url() so I'm still not sure what you're reporting as a bug. This is how Django works. It's not expected that the results of get_absolute_url() replace the /admin/... URLs, in the links, only that the redirect changes. Please explain what "pure mess" means (and try to avoid unfriendly language like that).

If there is some problem when enabling django-suit, then you need to report the bug to that package -- it's not part of Django.

comment:10 by George Tantiras, 8 years ago

Sorry, I have no intention to offend, I have corrected the inaccurate expression.

I understand from the docs that when it is needed to pass a url in the django view_on_site button , one can use get_absolute_url(self, obj) and preferably the reverse() method.

Assuming that the needed url is http://127.0.0.1:8000/test we can form it accordingly and return it.

This mechanism is not working either in the main model view or when the model is an inline. It seems to be no way, to alter the default http://127.0.0.1:8000/admin/r/19/3305/ that view_on_site returns.

I have used django-suit as an example to show that in the main model view, the desired url is indeed returned as django docs describe.

Version 0, edited 8 years ago by George Tantiras (next)

comment:11 by George Tantiras, 8 years ago

Finally, I sat in front of my box and found the solution to my problem. I was confused by django-suit which falsely returned an apparently correct result.

I had to use ModelAdmin.view_on_site(obj) instead of Model.get_absolute_url() due to the fact that the model was unaware of the custom ModelAdmin.get_queryset()I was using.

I feel like it is time to make an -at least- symbolic donation to redeem my lack of concentration. Thank you again for your patience and support!

Note: See TracTickets for help on using tickets.
Back to Top