Straightforward use of startproject command results in broken "Show on Site" link in admin
|Reported by:||Leo Hourvitz <leovitch@…>||Owned by:||nobody|
|Component:||Core (Management commands)||Version:||master|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
If you use startproject in the straightforward way (such as following the Django tutorial), and if you as suggested implement get_absolute_url() methods in your models, you will indeed get "Show on Site" links in your admin interface. Unfortunately, they'll be broken because they will redirect to example.com.
This is because the sites app is required, and thus activated; and it installs a default piece of SQL code that lists example.com as the URL for the project.
Obviously, one possible fix is to document an additional step everyone has to do (go into their db interface and edit the sites table) to set up any straightforward app, however there are two downsides to that: the first one is it makes the tutorial more complex. However, the stronger reason is that it introduces an absolute URL when a relative URL would generally be a better choice anyway (for instance, my Django apps have different hosts when running under test versus in deployment; that's why a relative URL is the better solution in most cases).
Fortunately, there's a simpler fix that seems to work just fine. Right now the settings.py file installed by startproject sets SITE_ID to 1. Per some earlier changelists, having this value set is required; but it's not required to set it to a valid site_id. If this setting is changed to any value that doesn't exist, the code in management handles the situation perfectly, and reverts to using the relative URL provided by the model's get_absolute_url method (almost certainly what everyone wants until/unless they're actually taking advantage of the sites application). Thus, the suggested fix is, in django/conf/settings.py, change the current uncommented line:
SITE_ID = 1
to something like:
# Right now, this is set to an invalid ID to show you're not using the sites app.
SITE_ID = -1
Obviously not the most important bug in the world, but it's good to get all the little stumbling blocks out of the way of new users whenever we can!
Change History (13)
comment:1 Changed 6 years ago by Leo Hourvitz
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:5 Changed 6 years ago by Simon G <dev@…>
- Summary changed from Startforward use of startproject command results in broken "Show on Site" link in admin to Straightforward use of startproject command results in broken "Show on Site" link in admin
- Triage Stage changed from Unreviewed to Design decision needed
comment:7 Changed 4 years ago by mtredinnick
- Triage Stage changed from Design decision needed to Accepted