1

I would like disqus comments disabled when my django app runs locally or on the test server.

So in my Django settings.py I am using:

DEBUG = True
DISQUS = False

Now in my django blog_post.html template I have an IF statement:

{% if DISQUS %}
   <div id="disqus_thread"></div>
    <script>
         etc.
         etc.
    </script>
{% else %}
   <p>Disqus disabled...</p>
{% endif %}

When I push the code to the live server (DISQUS = True) the comment section will be there in my html page.

While I was using Apache mod_wsgi I had no trouble with this setup!

However after switching to Passenger this stopped working. I have tried almost every known combination with no hope. The DISQUS custom variable will not get passed to the template.

Why is it working with mod_wsgi and not with mod_passenger?

Apache/2.4.6 (CentOS) Phusion_Passenger/5.0.28

Thanks

ionescu77
  • 1,150
  • 10
  • 14
  • Where is the code that is passing this variable to the template? – Daniel Roseman Jun 24 '16 at 23:05
  • "Django" is reading the settings.py and variables herein, which are then available for use in the templates. At least this is what used to work for me in another context. With apache mod_wsgi, but not anymore with passenger. – ionescu77 Jun 24 '16 at 23:10
  • 1
    Actually I think I found the pain-point. I have found this similar topic on stackoverflow [Django settings.py variables in templates](http://stackoverflow.com/questions/12166537/django-settings-py-variables-in-templates?rq=1). I will check and update the answer. – ionescu77 Jun 24 '16 at 23:33

1 Answers1

1

Stupid me! Daniel's comment made me think again about this.

I even found a similar 3y old issue and also a better reference in another thread about accessing constants in settings.py from templates in Django here on stackoverflow which better describes the problem.

Since Django 1.8 things got even simpler (you can also check Upgrading templates to Django 1.8).


I switched to the django app directory, where I created the file context_processors.py:

from django.conf import settings

def disqus(context):
  return {'DISQUS': settings.DISQUS}



I then opened settings.py where the TEMPLATES are defined and added this to the context_processors list:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(os.path.dirname(BASE_DIR), 'templates')],
        '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',
                'blogengine.context_processors.disqus',  # <--- add this line here

            ],
        },
    },
]


Now I can set DISQUS to True or False in settings.py and the html templates will check the status and show or hide the comments section.

Cool

Community
  • 1
  • 1
ionescu77
  • 1,150
  • 10
  • 14