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.
Related
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 ^_^
I want to use R from django app .Now I am in a huge mess .
--> I have installed rpy2 for that .
---> I am able to run everything from Python IDE
eg .
import rpy2.rinterface as rinterface
rinterface.initr()
or
import rpy2.robjects as something
As I open Python from cmd or in django file .I am getting error R_USER not defined
I am able to write separate .py files and access R but not from django and python shell .
Please help me Out!!!!
Or please tell what else I can use to call R functions from Python
Just create a new system variable R_USER in Environment Variables, with its value being the current user name, and the problem should goes away.
Note, this is clearly for windows platform only.
Otherwise you won't get R_USER exception in the first place.
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
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.
Using TextMate:
Is it possible to assign a shortcut to preview/refresh the currently edited HTML document in, say, Firefox, without having to first hit Save?
I'm looking for the same functionality as TextMate's built-in Web Preview window, but I'd prefer an external browser instead of TextMate's. (Mainly in order to use a JavaScript console such as Firebug for instance).
Would it be possible to pipe the currently unsaved document through the shell and then preview in Firefox. And if so, is there anyone having a TextMate command for this, willing to share it?
Not trivially. The easiest way would be to write the current file to the temp dir, then launch that file.. but, this would break any relative links (images, scripts, CSS files)
Add a bundle:
Input: Entire Document
Output: Discard
Scope Selector: source.html
And the script:
#!/usr/bin/env python2.5
import os
import sys
import random
import tempfile
import subprocess
fname = os.environ.get("TM_FILEPATH", "Untitled %s.html" % random.randint(100, 1000))
fcontent = sys.stdin.read()
fd, name = tempfile.mkstemp()
print name
open(name, "w+").write(fcontent)
print subprocess.Popen(["open", "-a", "Firefox", name]).communicate()
As I said, that wont work with relative resource links, which is probably a big problem.. Another option is to modify the following line of code, from the exiting "Refresh Browsers" command:
osascript <<'APPLESCRIPT'
tell app "Firefox" to Get URL "JavaScript:window.location.reload();" inside window 1
APPLESCRIPT
Instead of having the javascript reload the page, it could clear it, and write the current document using a series of document.write() calls. The problem with this is you can't guarantee the current document is the one you want to replace.. Windows 1 could have changed to another site etc, especially with tabbed browsing..
Finally, an option that doesn't have a huge drawback: Use version control, particularly one of the "distributed" ones, where you don't have to send your changes to a remote server - git, mercurial, darcs, bazaar etc (all have TextMate integration also)
If your code is in version control, it doesn't matter if you save before previewing, you can also always go back to your last-commited version if you break something and lose the undo buffer.
Here's something that you can use and just replace "Safari" with "Firefox":
http://wiki.macromates.com/Main/Howtos#SafariPreview
Open the Bundle Editor (control + option + command + B)
Scroll to the HTML Bundle and expand the tree
Select "Open Document in Running Browser(s)"
Assign Activation Key Equivalent (shortcut)
Close the bundle editor
I don't think this is possible. You can however enable the 'atomic saves' option so every time you alt tab to Firefox your project is saved.
If you ever find a solution to have a proper Firefox live preview, let us know.