Automatically run Django project in Debug when in PyCharm - django

Is it possible to run my Django project in DEBUG mode, when in PyCharm? If not in PyCharm, say in production, then of course the DEBUG mode should not be True. How can this be done most neatly?

The easiest way would probably be setting up your settings.py to read the DEBUG value from the environment variables, then tell PyCharm to run with that env var set.
import os
# ...
DEBUG = (True if os.environ.get('DEBUG') else False)
and set DEBUG=1 in PyCharm's Edit Configurations... window.

Edit your run configuration and set an environment variable called DJANGO_DEBUG to True. The easiest way to do this is to create a custom run configuration.
Run > Edit Configurations
Create a new configuration for django, by clicking on + and selecting django server:
Then, set a custom environment variable. You do this by clicking on the button next to PYTHONUNBUFFERED=1:
Next, add a new configuration variable by clicking the +
Click OK, and give your new configuration a name:
Click Apply then OK to dismiss the dialogues.
Edit your settings.py and add this:
import os
DEBUG = os.environ.get('DJANGO_DEBUG', False)
Now, simply select your new configuration from the Run menu when you want to run with debugging enabled.

The standard approach for this is to have multiple Django settings files:
settings/
base.py
production.py
development.py
testing.py
then you can run DJANGO_SETTINGS_MODULE=settings.development manage.py runserver.
PyCharm allows to pick the settings in run configurations too.

Use cookiecutter-duango to generate the project template when starting a new Django project. Your project can be configured for development with PyCharm and you get the both configurations, development (with DEBUG=True) and production (with DEBUG=False) automatically.

Related

How to run app in debug mode with Flask

I've setup a logger with Flask but it doesn't print to the Pycharm console, thought it might be because of not being in debug mode. app.debug = True but FLASK_DEBUG = 0 when I run the application.
app = Flask(__name__)
app.debug = True
you can get it done by visiting the flask edit configuration tab..I have attached the screenshots..please follow it..
visit the flask configuration tab by finding this option in your top right corner
Here you can enable disable debug option for your flask app
Make sure you've acquired the environment variable FLASK_DEBUG in your running environment.
on Unix $ export FLASK_DEBUG=1
on Windows set FLASK_DEBUG=1
Also app.run(debug=False, threaded=True) worked in Python2.6 ... worth a shot.
This also causes your app to restart when changes are detected in the application's source file.
Try this settings from official guide https://flask.palletsprojects.com/en/2.2.x/cli/#pycharm-integration
or this answer https://stackoverflow.com/a/73427798/17176270

Debuging Django Manage.py Custom commands Pycharm

I am trying to learn how to debug a django application with PyCharm. In the application we have several custom manage.py commands. If I run there via terminal (external or Pycharm's terminal) they run fine.
If I try to run them from PyCharm (Tool -> run manage.py task) so I can debug, I get the following error:
Unknown command: 'add_question'
Type 'manage.py help' for usage.
I started working on the Django Project before using PyCharm, so I created it via django-admin, and we are adding it to pycharm as OpenDirectory->Select the project.
Any help would be helpful, be it to manage to run the command from inside PyCharm, or to connect PyCharms terminal to the debugger so that I can debug when running from PyCharms terminal.
The solution is in another answer, though a little outdated. Quick update for PyCharm 2016.3:
Run > Run... > Edit Configurations... (Note: you can add configurations by doing both "Run..." and "Debug...")
Click green + (plus sign)
In the drop down menu, choose Django Server.
Check "Custom run command" and enter your command (the part that goes after manage.py in the console.
Make sure "Port" and "Host" are empty. If not—make them empty.
Change "Name" to your liking.
"Apply" and "Close"
Now you can run this configuration (Run > Run... > Your_configuration_name) and debug it (Run > Debug... > Your_configuration_name).
You can create a configuration for a custom command you made
Select Tools->Edit Configurations.
In the left column, you will find a list and a "+" button in the upper part. Click this one, then select "Django Server.
In the right part of the same window, you check "Custom run command", and write your custom command in the box
Write a name for this configuration and select OK.
Now just run/debug, selecting the configuration you just created.
for anyone still having trouble with this. Try setting up a python run configuration which is more flexible instead of django server configuration. Checkout this post

Pycharm error: Improperly configured

After having an unexpected shutdown on my DEV machine, when going back to Pycharm project, I noticed the Django view file I was editing (which had 700+ lines) when that happened, it was completely empty. I managed to restore it from a backup; no loss there.
The problem comes up when trying to debug, it returns this error: "ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings." Process finished with exit code 137
The Pycharm settings Django Support (project root, settings & manage script) have the expected values as well.
If I run the project with the ./manage .py runserver command, everything is fine. I can even access the DB with manage.py dbshell. I looked at my settings file and everything seems OK. I also updated from version 3.0.1 to 3.1.1, and no difference.
I'm using Django 1.6.1 and postgresql 9.2.7. What can I do?
For Pycharm, just go to Run -> Edit Configurations, select your project on the right of the window from Debug Configuration, and you will see Environment variables on the right. Make sure you have set DJANGO_SETTINGS_MODULE=mysite.settings, if not just add one, it is as easy as fill a key value pair from the pop up dialog.
I'm using PyCharm Professional and the answers provided here didn't work for me.
I went to Build, Execution, Deployment -> Consule -> Django Console and then added DJANGO_SETTINGS_MODULE=my_app_name.settings to Environment Variables.

Pycharm runserver error with custom settings.py files

just started using pycharm on a existing project, and having difficulty using the run/debug configurations in order to run the test server. The issue stems from their being no settings.py module as standard in django projects. Instead there is settings_base.py and then two different settings modules (settings_live.py and settings_test.py) that extend it by using 'from settings_base import *'. This causes manage.py to fail when running runserver because it can't find settings.py.
In the Django Support settings I have set the project root, and set Settings to settings_test.py however this has not helped. In the Django server run configuration I have setup I also have...
DJANGO_SETTINGS_MODULE=settings_test
... in the Environment Variables section. However when I use runserver it still says
Error: Can't find the file 'settings.py' in the directory containing '/home/pete/Projects/the_project/manage.py'. It appears you've customized things.
You'll have to run django-admin.py, passing it your settings module.
(If the file settings.py does indeed exist, it's causing an ImportError somehow.)
I tried wrapping the entire settings_test.py module in a try/except to see if it really was an import error, however it did not seem to work. Is there something I am missing?
Maybe you should try adding --settings=settings_test to the "Additional options" in your PyCharm Run configuration and make sure that "Working directory" points to correct path.

how to deploy / deployment django projects / application with aptana to webserver apache wsgi

I would like to deplay a django application/project, which i have created within Aptana. It is a simple hello world. I can run the application locally and everything works fine, but how to deploy it to a webserver. The deploy button is grey and not working in the PyDev Perspective.
If i switch to perspective "Web Perspective" i can download(check out from a specific path with password) django applications from a webserver, edit files, and upload again. I would like to use the functionality from the web perspective in my pydev perspective where i write my django code.
But when i create my own django project, the button deploy is grey.
This is now my solution to deploy django 1.3 with aptana to Apache Server:
http://i.imgur.com/FH0E2.jpg
Start Aptana, select PyDev Perspective (right corner) and just create a simple Django Project
Create a views.py ane a method index()
def index(request):
return HttpResponse("Hello world")
Edit your urls.py so that django can find the method index()
Run Sync DB (rightclick your project in PyDev Package Explorer, at bottom click Django, and click Sync DB). I am using here a local sqlite3 db
if everything work,s you will see within Aptana Console: Finished "/home/xy/workspace/test1/test1/manage.py syncdb" execution.
Now Run local your django app: rightclick your project on left side of aptana (PyDev Package Explorer) somewhere in the middle you can see RUN AS: 1 PyDev:Django
Now open your browser: http://127.0.0.1:8000/index or http://127.0.0.1:8000
U see now your "Hello World"
If this worked, now lets deploy to apache:
switch to Web Perspective (right corner of Aptana)
switch to App Explorer (left side of Aptana) IMPORTANT!!!!!
there is a little dropdown menu: select there your application (e.g. test1), BUT do not select any of the packages or modules inside of your project!!
you will see now your app and nothing is selected/clicked=> you see also a little box above!
click it and Run Web Deployment Wizard!!! if anything in your package is selected, you will not see "Run Web Deployment Wizard".... (strange)
Select SFTP Protocol; Fill out form
Remote Path: before you can select the directory you want to have your app uploaded, you should create it on the server
After you have selected the directory where you want to have your app deployed to server (ubuntu) you can again click on the little box and select deploy app
Now you get a new window SYNCHRONIZE
Your data/app have been now uploaded to server in the chosen directory
I use wsgi so i have to edit 2 files
the first one is a file called test1.wsgi in the wsgi directory
import os
import sys
path = "/home/username/"
if path not in sys.path:
sys.path.append(path)
path = "/home/username/somedirectory/test1/test1/"
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi application =
django.core.handlers.wsgi.WSGIHandler()
now go as sudo -s to cd /etc/apache2/conf.d/
create a file test1
write
WSGIScriptAlias /rofltest /home/username/wsgi/test1.wsgi
/rofltest means www.youraddress.org/rofltest will be the url
/home/username/wsgi/test1.wsgi means where to look for a file which will tell where to look for your django app on the server
restart apache (/etc/init.d/apache2 restart) and browse to www.youraddress.org//rofltest/index
You see Hello World or an error because you have to adjust the paths.
Start with settings.py and edit ROOT_URLCONF = 'test1.urls' maybe just to 'urls'
now it works :)
Try it, if it fails, write a comment and maybe i read it and help. If this helped, upvote it.
It doesnt work for me. Even when I extra care about not selecting the project in the App Explorer, the small "publish" icon is enabled, but the dropdown beside it is empty and nothing happens when I click it.
Actually there is a known bug when the Project root is a source folder, see https://jira.appcelerator.org/browse/APSTUD-2543