9👍
Putting
'django.contrib.sites',
into your INSTALLED_APPS and a following
$ ./manage.py syncdb
may suffice.
When installed, edit the Site instance (e.g. through /admin
interface) to reflect your local hostname (e.g. localhost:8000
).
15👍
Define a get_absolute_url
on your model. The admin uses that method to figure out how to construct the objects url. See the docs.
- Django: JSON Notifications using Redis PubSub, Node.js & Socket.io
- Django.db.utils.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
- Can't pip install mysql-python
- How to send success message if we use django generic views
- How to unit test methods inside django's class based views?
7👍
As communicated by others, this requires a couple extra steps in addition to enabling view_on_site. You have to implement get_absolute_url() in your model, and enable Sites in your project settings.
Set the view_on_site setting
Add view_on_site setting to admin form:
class MymodelAdmin(admin.ModelAdmin):
...
view_on_site = True
...
admin.site.register(Mymodel, MymodelAdmin)
Implement get_absolute_url()
Add get_absolute_url() to your model.
In models.py:
Mymodel(models.Model):
...
def get_absolute_url(self):
return "/mystuff/%i" % self.id
Enable Sites
Add Sites in yourapp/settings.py:
INSTALLED_APPS = (
...
'django.contrib.sites',
...
)
Then update the database:
$ python manage.py migrate
Done!
Check out reverse() for a more sophisticated way to generate the path in get_absolute_url().
- Use of unicode in Django
- Django Queryset to dict for use in json
- Why isn't my Django User Model's Password Hashed?
- Django check if checkbox is selected
- Django – UpdateView with inline formsets trying to save duplicate records?
2👍
According to the Django documentation, and as of Django 3.1 (May 2020), you have to define a get_absolute_url() method in your model.
One place Django uses get_absolute_url() is in the admin app. If an
object defines this method, the object-editing page will have a “View
on site” link that will jump you directly to the object’s public view,
as given by get_absolute_url().
Here is an example from the documentation:
def get_absolute_url(self):
from django.urls import reverse
return reverse('people.views.details', args=[str(self.id)])
Source: https://docs.djangoproject.com/en/3.1/ref/models/instances/#get-absolute-url
- Django static files not working
- How do you get Django to make a RESTful call?
- How to downgrade from Django 1.7 to Django 1.6
1👍
It seems to me that the view on site functionality works only if get_absolute_url
refares to a Django view. It does not seem to work if you are trying to create a link, which redirects to a page out of Django’s control (even if it is served from the same domain by apache itself).
In this case, it is easy to create the button manually by overriding admin tempale as follows:
{% extends "admin/change_form.html" %}
{% block object-tools-items %}
{{ block.super }}
<li>
<a class="viewsitelink" href="{{ original.get_absolute_url }}">View on my site, out of Django's control</a>
</li>
{% endblock %}
Also, add view_on_site = False
to your ModelAdmin
class, otherwise both of the buttons will appear.
- Django – How to send a success message using a UpdateView CBV
- Django: Tweaking @login_required decorator
- How does this Man-In-The-Middle attack work?
- Display query string values in django templates
0👍
When you have edited either SITE_ID in settings.py or a Site instance thought the admin, don’t forget to restart your web server for the change to take effect.
- Nullable TextField or empty string in Django model?
- How to use forms in django-cms?
- How to display "This many months ago" in Django using Humanize?