25👍
Alright I fixed it, this was a stupid mistake but I was already playing with this for the past 2 hours. I had to declare my app before django.contrib.admin. It wouldn’t accept it otherwise.
23👍
One more mistake that one should resist making on this exercise. The exercise says to change this…
<h1 id="site-name"><a href="{% url 'admin:index' %}"> {{ site_header|default:_('Django administration') }} </a></h1>
to this…
<h1 id="site-name"><a href="{% url 'admin:index' %}">Polls Administration</a></h1>
There is a temptation to only change the string constant, but that is incorrect. I.e. do NOT do this, it will not alter the heading:
<h1 id="site-name"><a href="{% url 'admin:index' %}"> {{ site_header|default:_('Polls Administration') }} </a></h1>
That was the mistake I made, and I had to go through the exercise meticulously to fix it.
- [Django]-Django AutoField with primary_key vs default pk
- [Django]-'staticfiles' is not a valid tag library: Template library staticfiles not found
- [Django]-405 "Method POST is not allowed" in Django REST framework
8👍
I couldn’t get the admin template to be recognized when doing this step in part two of the Django tutorial.
This is how I solved it:
Using the information in this answer, I printed the value of TEMPLATE_DIRS
:
- At the command line, navigate to the directory in which the project’s
settings.py
file exists (for me this isR:\jeffy\programming\sandbox\python\django\tutorial\mysite\mysite\
) - Start the interactive shell:
python
>>> import settings
>>> settings.TEMPLATE_DIRS
, which outputs['R:\\jeffy\\programming\\sandbox\\python\\django\\tutorial\\mysite\\templates']
So this is the expected location of the templates directory for this project. The admin directory goes into that, and the base_site.html
file goes into that.
The other problem I had was that it was working, but I only changed the <TITLE>
field, so I just didn’t notice it. (I thought I was changing the main header.)
- [Django]-Where to put business logic in django
- [Django]-Cross domain at axios
- [Django]-Add a custom button to a Django application's admin page
6👍
I had this same problem walking through the Django 1.6.5 tutorial (https://docs.djangoproject.com/en/1.8/intro/tutorial02/), but realized it was a mistake in not reading carefully. The tutorial has you do is put this into your settings.py:
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
...
}
I then put the templates folder I wanted to put override for the admin site under the app just like your example:
/project_folder/
manage.py
settings.py
urls.py
__init__.py
/app/
views.py
models.py
__init__.py
/templates/
/admin/
base_site.html
With this it wouldn’t work for the reason similar to the issue you had noted by Daniel Roseman in one of the comments, my DIR
was evaluating to project_folder/templates
(as I told it to). Then I noticed in the tutorial it explicitly said put the templates/admin
folder at the project level, not the app level:
Create a templates directory in your project directory (the one that contains manage.py). Templates can live anywhere on your filesystem that Django can access. (Django runs as whatever user your server runs.) However, keeping your templates within the project is a good convention to follow.
By doing this I had the following structure:
/project_folder/
manage.py
settings.py
urls.py
__init__.py
/templates/
/admin/
base_site.html
/app/
views.py
models.py
__init__.py
With this, the everything worked as expected (I could overwrite the default django templates for the admin pages).
While you should be able to put templates anywhere and configure Django to find them, it seems to makes sense to put your main admin templates at the project level, as the admin site’s not app specific, but available for the entire project.
- [Django]-Combining Django F, Value and a dict to annotate a queryset
- [Django]-Rendering a value as text instead of field inside a Django Form
- [Django]-Django Rest Framework model serializer with out unique together validation
- [Django]-South migration: "database backend does not accept 0 as a value for AutoField" (mysql)
- [Django]-Django render_to_string missing information
- [Django]-Is this the right way to do dependency injection in Django?