Fantomas42 / django-blog-zinnia

Simple yet powerful and really extendable application for managing a blog within your Django Web site.
http://django-blog-zinnia.com/
BSD 3-Clause "New" or "Revised" License
2.12k stars 730 forks source link

Overriding templates #359

Closed shawn-simon-developer closed 9 years ago

shawn-simon-developer commented 10 years ago

I've been trying to a bit to get Zinnia working on an existing Django website. Everything is finally up and running but I'm having trouble overriding the templates. For example, Zinnia uses base.html and that includes the {% block sidebar %} but when I go to override it, the changes I make are never rendered on the page.

I've installed the apptemplates loader and added 'apptemplates.Loader', to my TEMPLATE_LOADERS. I've also tried using the django-app-namespace-template-loader the same way, with no results.

Additionally I have tried extending them using django overextends.

I've also ensured that I have the template directory I'm working with (templates/) added to my TEMPLATE_DIRS, and the directory structure looks like this:

templates/

       classfied/                             <- Another custom app
             some_other_templates.html

       zinnia/
             base.html

My base.html looks like this:

{% extends "zinnia:zinnia/base.html" %}
{% load zinnia_tags i18n %}

{% block sidebar %} 

  <p> OVERRIDED SIDEBAR </p>

{% endblock sidebar %}

But alas, the sidebar is not changed. It's probably something small that I've missed, but I'm having a hard time debugging it. Any help is greatly appreciated. Let me know if you need any more information!

This isn't really an issue but hopefully it gets flagged as a question.

Thank you!

bittner commented 10 years ago

I've explained overriding the templates on the mailing list a couple of months ago. See if this helps you. Alternatively, please check out how I override the templates in the django-organice-theme project, though I believe that's exactly the same way.

You have to get the directory structure right: your_project/templates/zinnia/base.html should work with a simple {% extends "base.html" %}. Your TEMPLATE_DIRS must point to the right folders and contain absolute paths, e.g.

TEMPLATE_DIRS = (
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(PROJECT_PATH, 'templates'),
    os.path.join(PROJECT_PATH, 'templates', 'zinnia'),
)

with PROJECT_PATH = os.path.abspath(os.path.dirname(__file__)) or so, just to make it a bit more maintainable.

If that all doesn't help it may be a good idea to post the relevant parts of your settings file here.

shawn-simon-developer commented 10 years ago

@bittner sorry I didn't mean to leave you the settings information! The app is called classified

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

TEMPLATE_DIRS = (
os.path.join(BASE_DIR,'..', 'classified/templates/'),
os.path.join(BASE_DIR,'..', 'classified/templates/zinnia'),
)

TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'app_namespace.Loader', 
)

And in my base.html which is in classified/template/zinnia/

{% extends "base.html" %}
{% load zinnia_tags i18n %}

{% block sidebar %}
Change.
{% endblock %}

EDIT: removed namespace thing from before/app name description.

bittner commented 10 years ago

Please try

BASE_DIR = os.path.abspath(os.path.dirname(__file__))

Your double-dirname version yields an empty string (dunno if that's just a typo). Also, consistently use os.path.join instead of the slashes, e.g.

os.path.join(BASE_DIR, '..', 'classified', 'templates', 'zinnia'),
shawn-simon-developer commented 10 years ago

@bittner made the change. Sadly template is still not updating. :(

This is a collaborative project so if I occasionally take a while to respond it is because I am asking a question to a team member.

Any more information I can give you that might help debug this?

bittner commented 10 years ago
  1. Please look at the mailing list post and the implementation of django-organice-theme as I mentioned above, and compare it line by line with your code. (If your code is closed source you have to do the tedious work!)
  2. Make sure DEBUG = True in your settings, then put a line into one of Zinnia's templates :flashlight: which causes Django to fail (syntax error or so). That will show you which templates were loaded right in your browser, and may help you further.
  3. Look at 1. again. If you don't you're screwed.

:flashlight: They should be in your $WORKON_HOME/<your_project>/lib/python*/site-packages/zinnia/templates/ (assuming you're using virtualenv).

What do you use for debugging? Look at some choices in this StackOverflow question. And don't worry for responding: I'm on the other side of the ocean, 6+ hours time difference.

shawn-simon-developer commented 10 years ago

Thank you ! I will get on this now and let you know here when I get results.

githubwoniu commented 8 years ago

@shawn-simon-developer @Fantomas42 @bittner I think the true answer is in django 1.8, we should set DIRS=[] of TEMPLATES in settings.py instead of TEMPLATE_DIRS, here is my solution

TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [ os.path.join(BASE_DIR,'blog','templates'), os.path.join(BASE_DIR,'blog', 'templates', 'zinnia'), ],

'APP_DIRS': True,

    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
             'zinnia.context_processors.version',  # Optional
        ],
        'loaders': [
                            'app_namespace.Loader',
            'django.template.loaders.filesystem.Loader',
                            'django.template.loaders.app_directories.Loader',
        ],
    },
},

]

wish this could help. Best regards.