Custom base_site.html not working in Django - django

I am using Nitrous for playing with the Django framework. In tutorial 2 is shown how to change the base_site.html template. I've added in the TEMPLATE_DIRS = ( ) a new line:
'home/action/workspace/mysite/templates',
And in base_site.html I changed the site name title of Django Administration into Administration:
{% trans 'Administration' %}
But I still see no changes on the website. I've tried different TEMPLATE_DIRS like:
'~/workspace/mysite/templates',
'home/action/workspace/mysite/',
'home/action/workspace/mysite/templates/',
And restarting the server. But I am doing something wrong.

Note that this answer was written before the TEMPLATES setting was introduced in Django 1.8.
The tutorial says you should create an admin subdirectory inside your template directory, and put your template inside that.
Try with base_site.html in /home/action/workspace/mysite/templates/admin and with
TEMPLATE_DIRS = (
'/home/action/workspace/mysite/templates',
)
Note the slash before /home/... and the trailing comma to make it a tuple.

Related

change verbose name of VISIT SITE in django 2.x admin

I only seem to find answers around changing the actual link of VISIT PAGE. How however can I change the verbose name to e.g. HOMEPAGE?
Below code only changes the actual link:
class AdminSite:
admin.site.site_url = "/hompeage"
What I'd like to achieve:
I'm looking to achieve this in the admin.py file of my application.
I haven't personally tried any ways, but looking the code
You can do it in two ways,
Override the base.html template on admin.
See: Django Github base.html
{% translate 'View site' %}
to
HomePage
If you look the code, it uses translation for
{% translate 'View site' %}
You can simply, override the locale string too.
See: Django Github en locale

Django Templates: How does it decide where to load a template?

I was following the Django Girls Tutorial up to the point where we add the login capabilities and I've gotten to a point where it tries to load from a different template folder than I want.
I have all my blog templates in blog\templates\blog\, etc. and that is where Django is looking for my login.html. However, in the tutorial (and as a result in my folder structures), the path for login.html is: mysite/templates/registration/login.html. So how would I make Django look there?
Sorry if this is a stupid question.
Django will look to your settings.py for how to load templates, and the order in which it should try to load templates. It's likely that you haven't configured django to look for templates in mysite/templates.
There's a setting called TEMPLATE_DIRS which is a list of absolute paths for your template folders. So in your settings.py file, try something like below. Read my comments to see what each line does.
# create a variable that stores the absolute path to your project's root directory
# you might have something like this already defined at the top of your settings.py
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
# this is the default value for TEMPLATE_LOADERS
# which says to look at the `TEMPLATE_DIRS` variable, and then in each of your app's subdirectories for templates
TEMPLATE_LOADERS = ('django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader')
# the special sauce. tell django to look at the "templates" folder
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'), )

Running Django site with a URL prefix

I'm trying to put my Django site in a subpath, say www.example.com/mysite/ but I can't get it to work 100%.
I read up on the answer Django Apache Redirect Problem , where it is suggested to just change the site in the admin from www.example.com to www.example.com/mysite/, which is exactly what I want to do and it almost works. All urls in the main urls.py gets redirected properly, but everything in the includes drop the "mysite" directive when using the links in the templates (one example is {% url journal_index %} which after the change should go to www.example.com/mysite/journal but goes go www.example.com/journal/).
Hope this makes sense.
Cheers!
Try adding the FORCE_SCRIPT_NAME setting:
FORCE_SCRIPT_NAME = '/mysite'

django url NoReverseMatch works in 3rd party app but not my project

I am integrating django-zinnia 0.12.3 into my django 1.4 project. I have it installed and working. I want to override its zinnia/base.html template so that all the content appears using my site's template. The zinnia templates extend 'zinnia/base.html'.
When I copy the file zinnia/templates/zinnia/base.html to myproject/templates/zinnia/base.html all the zinnia {% url %} stopped working and gave NoReverseMatch. Even if I made zero changes to the file. For example:
{% url 'zinnia_entry_archive_index' %} --> returns:
NoReverseMatch Reverse for ''zinnia_entry_archive_index'' ... not found
{% url 'admin:app_list' 'zinnia' %}" title="{% trans "Dashboard" %} --> returns:
NoReverseMatch u"'admin" is not a registered namespace
I was solved the problem by removing the quotes around the url names, for example:
{% url zinnia_entry_archive_index %}
However, if I remove the copy of base.html that I put in myproject/templates/zinnia (in other words it uses the original one in the zinnia project), the urls work again.
My question is why does it work with the quotes inside the original zinnia folder, but not from within my project folder?
The reason is that in Django <= 3 the url tag takes url name without quotes. But in Django 1.4+ it is deprecated and url name without quotes is gone in Django 1.5:
So if you are using Django <= 1.4 do not remove the quotes (unless you are passing context variable) around url names. Instead do this for compatibility reason if you ever wanted to upgrade your django version:
{% load url from future %}
{% url 'zinnia_entry_archive_index' %}
Documentation
Don’t forget to put quotes around the function path or pattern name!
Changed in Django 1.5: The first parameter used not to be quoted, which was inconsistent > with other template tags. Since Django 1.5, it is evaluated according to the usual rules: > it can be a quoted string or a variable that will be looked up in the context.

Difficulty overriding Django Admin template

I'm using Django 1.2.4 on Ubuntu 10.10. I'm trying to override the index.html template for the admin module. I've been following these instructions. I also looked at this question, but I'm still having difficulty.
The instructions say to create an admin directory in the templates directory:
templates/
admin/
index.html
I want to override a single block in the index.html. (Really, all I want to do is append some text to the end. Is there an easier way than copy/pasting the entire block and changing it?) (Update: Looks like {{block.super}} may help.)
To signal that I'm overriding, I put this at the top of my index.html:
{% extends "admin/index.html" %}
Of course, that results in a stack overflow (from the terminal):
Exception RuntimeError: 'maximum recursion depth exceeded in __subclasscheck__' in <type 'exceptions.RuntimeError'> ignored
What is the correct way to do this? I tried a symlink per an answer on the linked question, but that resulted in the following:
me#mycomp:~/foo$ sudo ln -s /usr/local/lib/python2.6/dist-packages/django/contrib/admin/templates/ django_admin
[sudo] password for me:
ln: creating symbolic link `django_admin': Protocol error
What am I doing wrong?
The recursion error is because you're extending the admin/index.html with itself.
You can either:
copy the entire admin/index.html template in your templates/admin/ directory, and it will replace the default template with yours
override the index.html per app or model, as explained here
I know this is late after the question, but you know, google traveling...
Amend settings.py with an extra template folder, for example:
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
"/home/mysite/webapps/django/myproject/templates",
"/home/mysite/webapps/django/lib/python2.7/django/", # extra folder
)
Then in myproject/templates/admin add your own index.html like:
{% extends "contrib/admin/templates/admin/index.html" %}
{% block branding %}
<h1 id="site-name">Administration for TheLittleButtonCo</h1>
{% endblock %}
Variations are possible, obviously. This works on Django 1.3.1 final
Not sure if you found the answer, but you need to change
{% extends "admin/index.html" %}
to
{% extends "admin/base_site.html" %}
as that is what the original index.html page overwrites. Because the Django system searches your templates folder before using the default admin one, so in this case it finds the admin/index.html in your templates, then it's trying to extend itself with the extend (hence the recursion error).
For reference you can customise the base_site.html in you templates too, it extends base.html. The best thing to do is copy the original from:
/usr/local/lib/python2.6/dist-packages/django/contrib/admin/templates/
and paste it into your templates folder as a starting point
I use an extra package, called django-smart-extends