S
S
Sergey08082017-10-06 12:25:13
Django
Sergey0808, 2017-10-06 12:25:13

Python Django, how to set a secret link in the admin panel?

There is a booleanfield field in the news model, in which if the checkbox is checked, a hash is generated, which is a unique parameter in the URL for a separate news item, if the checkbox is not checked or unchecked, the link is deleted. It all works, but one hash is generated. For example, this one is c63f96d367536fa10914963237616627. But I need the full address to be generated - 127.0.0.1:8000/secret/c63f9654d367536fa10914963237... and that only the hash is added to the model field, and the full address is displayed in the admin panel. How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NotProblem, 2017-10-06
@alexbog80

Will the link be followed only from the admin panel?
You can add a function to the admin class:

def get_hash_link(self, obj):
    try:
        return mark_safe(u'<a href="/secret/{0}" target="_blank">{0}</a>'.format(obj.hash))
    except:
        return u'Невозможно сформировать ссылку'

and then call her
or from the form
fieldsets = (
        (u'Основные данные', {'fields': (
            'field1',
            'field2',
            'field3',
            'get_hash_link',
            ...
        )}),

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question