Django DEBUG = True xlsx files do not work - django

I have faced a strange problem.
I have a button that creates xlsx file. For example, firstly file is empty and when I press the button it becomes full of information from the database( PostgreSQL ).
And now what is wrong:
When DEBUG is TRUE in settings.py file everything works pretty fine and the document creates. When DEBUG is FALSE it do not change the file.
I really appreciate all answers, thanks!

One of the main features of debug mode is the display of detailed error pages. If your app raises an exception when DEBUG is True, Django will display a detailed traceback, including a lot of metadata about your environment, such as all the currently defined Django settings (from settings.py).
It is also important to remember that when running with DEBUG turned on, Django will remember every SQL query it executes. This is useful when you’re debugging, but it’ll rapidly consume memory on a production server.
Finally, if DEBUG is False, you also need to properly set the ALLOWED_HOSTS setting. Failing to do so will result in all requests being returned as “Bad Request (400)”.
source: https://docs.djangoproject.com/en/dev/ref/settings/#debug ^_^

Related

javascript file loading problem in a web browser, relating to mime types, context Django debug_toolbar concerning Django/debug_toolbar versions >=3.X

I have noticed, at some point last year, that the debug_toolbar is not present anymore. Now I have found the problem, but do not actually completely understand what is happening and why. [Update: I have found an answer - see the last few paragraphs ]
The initial information is that I tried a lot of different version combinations for Django/delug_toolbar and there were no problems as the version number of both started by 2.
However, the toolbar is not showing itself if a version number starts by 3. The toolbar 2.2 should work with Django version 3 or larger also.
Now I have located the problem to be that toolbar.js is not loaded. There is a message in the developer tool's console:
:8001/static/debug_toolbar/js/toolbar.js:1 Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.
So I found the location of the toolbar.js under the static files folder of the debug_toolbar.
The file is loaded in a template base.html (in folder env\Lib\site-packages\debug_toolbar\templates\debug_toolbar)
There is a line:
<script type="module" src="{% static 'debug_toolbar/js/toolbar.js' %}" async></script>
But I do not understand why type="module" is preventing the browser to load and use the toolbar.js?
There was a section in the (https://readthedocs.org/projects/django-debug-toolbar/downloads/pdf/stable/) Django Debug Toolbar manual that said in case of the problem with mimetypes the user should edit Windows registry.
"1.6 Troubleshooting
On some platforms, the Django runserver command may use incorrect content types for static assets. To guess content types, Django relies on the mimetypes module from the Python standard library, which itself relies on the underlying platform’s map files. If you find improper content types for certain files, it is most likely that the platform’s map files are incorrect or need to be updated. This can be achieved, for example, by installing or updating the mailcap package on a Red Hat distribution, mime-support on a Debian distribution, or by editing the keys under HKEY_CLASSES_ROOT in the Windows registry"
Could someone please give me an explanation for what is going on here? I tried to find if there was any discussion about this problem with these new Django/Django Debug Tool versions but there was nothing to be found. The other discussion was concerning about the earlier versions.
Do I have a security setting of a browser (etc) that is causing the problem as there is no question about the people that have made the Debug toolbar would have implemented their code/settings not to work at all? I have tried Firefox/Edge/Chrome and the issue is in all of them.
I found a tip from another question.
By adding
if DEBUG:
import mimetypes
mimetypes.add_type("application/javascript", ".js", True)
to settings.py causes that the toolbar.js is loaded.
That addition solved the whole problem. There is no need to do anything for the Windows registry.
The discussion can be found here:
Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain". Strict MIME type checking i
So my question was in fact a duplicate.

Coldfusion per application mapping randomly fails

In my application.cfc file I have the following code:
this.mappings["/includes"] = "#expandPath('../../priv/inc/')#";
and then on a page I have the following CF include set up:
<cfinclude template="/includes/imageOptimise-thumbnail.cfm">
For the majority of the time when the page is submitted the imageOptimise-thumbnail page runs without any issue but occasionally it will error saying that it can't find the imageOptimise-thumbnail.cfm. If I resubmit the page, chances are it will work but the question is, why is it sometimes not being able to find the page?
I had a look in the CF Admin and I can't see anything in the logs saying that there was an error but it is clearly showing a Coldfusion 'template not found' error message when it fails.
Anyone got any ideas?
I think your issues stem from the fact that the path used for the mapping is relative.
According to advice in ColdFusion Mappings and Relative Paths,
the moral of the story is to NEVER use expandPath() to create a
mapping that’s relative to the webroot.
Try to make the webroot static as the blog suggests
<cfset this.mappings["/myapplication"] =getDirectoryFromPath(getCurrentTemplatePath())
and navigate to /priv/inc/ from the webroot

How to show variable data for debugging in django

I still cant figure out, how to debug in django best way. For example, I created a dict and now I would like to check all the data that is arranged in that dict. How can I force django to show a debugging page in browser and print out the dict in a comfortable way?
To get the variables value in browser using Django is possible naturally only if:
DEBUG=True is set for the app and some error occurs.
Instead you can use one of the following(if you already know the line/variables to debug):
print(dict_var)
From your view: return HttpResponse(dict_var)
Other way to debug is to use Python Debugger.
Add following line in your code to put a breakpoint and then debug line by line.
import pdb; pdb.set_trace()
And run your app using
python -m pdb manage.py runserver
PyCharm
You can use the debugger of PyCharm an ide developed by JetBrains.
It's free and it helps you in so many other way.
And if you are a student the premium version it's free.
Download Link
Edit:
Then run the program in debug mode and set a breakpoint.
To set a breakpoint just click the number of line in the left of the view.

Programmatically disable DEBUG in Django command

I am using a custom Django command to generate XML sitemaps for a site with about 3-4 million data entries (./manage.py generate_sitemaps). This seems to work, but eats way too much memory when DEBUG is enabled in settings.py.
I usually have the DEBUG option enabled during development and frequently forget to disable it before starting the sitemap creation. If that happens, the memory starts filling up until the script crashes after about 2-3 hours. Very annoying.
Is there a way to temporarily disable the debug setting for the execution of a Django command? I thought of importing the settings module and overriding the option, but I don't think that'll work.
I think you have a couple of options here:
Import settings and throw an error to remind yourself to turn debug off.
Use the --settings= and set that equal to a file (e.g. gen_settings.py) file specifically for your generate_sitemaps command where DEBUG=False. Then create an alias for ./manage.py generate_sitemaps --settings=gen_settings
http://docs.djangoproject.com/en/dev/topics/settings/ warns specifically to not change the settings at runtime
I've used the second option before and it worked fairly well. Better than being annoyed after 2-3 hours =)
I am not really sure it helps you, but you can try:
from django.conf import settings
tmp = settings.DEBUG
settings.DEBUG = False
# some your actions
# ...
# ...
settings.DEBUG = tmp
Alternatively you can use separated settings file and set it in command line like
./manage.py your_command --settings=another_settings.py
And in that another_settings.py:
from .settings import *
DEBUG = False

How to trace import of the views in Django

Is there a way to somehow trace importing of the views ? I want to find which one is broken and doesn't import in some situations (which leads to the fact that all url resolving schema in django stops working).
Pretty amazing that no one has suggested pdb. Place the following in a strategic point in your code:
import pdb;pdb.set_trace()
When execution reaches that point, the dev server will drop into a shell where you can check values of variables, trace the execution, etc.
It works like a standard shell (use any python commands you like), but there's also special commands that let you control the execution. For example next will go to the next line (processing the previous line). continue will continue execution until the next break point, etc. (full list of pdb commands)
Don't you get a stack trace? Is DEBUG set to True?
Ok, well it's possible to just write
python -v manage.py <whatevercommand>
and search for the error in the produced logs.
I'm assuming this means you're getting a 501 server error?
If you're using the Apache web server, you can set it to log python errors in the config for the site using the ErrorLog directive:
ErrorLog /tmp/django_errors.log
Then in the terminal (or via ssh):
tail -f /tmp/djanogo_errors.log
And then try to load the webpage in question. You should then be able to see what the error is and fix it.