How to run Django function in Pycharm? - django

I have some internal utilities I want to develop for my Django project. These are utilities that I need to run mostly in my DEV environment, not in production.
I want to be able to call these from my PyDev IDE or Terminal. Is there a way to call functionality in my Django project without creating a view and using a URL to access it?
I've done several Google searches, but nothing is coming up.

If you open the "Python Console" on Pycharm, it will automatically set django up and then you can basically do anything you could do on a Django view. You can write your function anywhere and import and call them. To do the setup manually, you'll need these for lines of code after launching a Python interpreter:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'path.to.your.settings')
import django
django.setup()
# Write code using Django models here

you could import them to your PyCharm console starting script. In settings under: Build, Execution, Deployment -> Console -> Python Console -> Starting script

Related

Harvard University CS50 Lecture on Flask

I am taking CS50 lecture on Python Flask
https://www.youtube.com/watch?v=oVA0fD13NGI&t=5449s
I am using PyCharm instead of VS Code. Here is my problem.
When I make changes in my PyCharm code and "Save All" I should be able to just "refresh" my browser to see the changes, (That's what the CS50 instructor can do), but I have to Stop and Start my Flask server and then go back to my browser in order to see the change.
Is there something I need to turn on and/or configure in PyCharm so that when I make code changes in a Flask application I can just refresh the browser without having to Stop/Start the Flask server. Thanks in advance for any help on this issue.
if you running your application with flask run in terminal, you can use --debug before run, to run in watch mode like this flask --debug run.
Have another away to run in watch mode.
You can use this code in your main file
if __name__ == "__main__":
app.run(debug=True)
when you run, you have to use python like this python app.py.
You can see more in the flask documents Here

compiled django application fails to run ASGI channels

I'm building a django application using Pyinstaller.
When I run the application i get an error from dapne server.
"daphne\server.py:13: UserWarning: Something has already installed a non-asyncio Twisted reactor. Attempting to uninstall it; you can fix this warning by importing daphne.server early in your codebase or finding the package that imports Twisted and importing it later on."
In addition, when I compare the console log of the EXE application and a regular run of the django application I notice the following difference.
In the regular run which works as expected I see the following print:
"Django version 2.1.5, using settings 'cfehome.settings'
Starting ASGI/Channels version 2.1.5 development server at http://0.0.0.0:8000/"
however, when I run the application from the EXE I see:
"Django version 2.1.5, using settings 'cfehome.settings'
Starting development server at http://0.0.0.0:8000/"
Appreciate any lead to understand this behavior and ways to fix it.
I know this is old but for others seeking answer. There's a simple fix:
Before importing raven in settings.py, import daphne.server So, your settings.py will look like this:
import daphne.server
import raven
For further information you can read: https://github.com/django/channels/issues/793
I hope it will help.

How to work with Django using Jupyter notebook

I'm trying to work in Django using Jupyter notebook. Could someone guide me in how to start working with Django.
I have installed all the necessary components like django, django-extensions and django-admin and other plugins using pip, as well as checking the INSTALLED_APPS and MIDDLEWARE in settings. I couldn't get to know, how to start working in a project. I normally work in Jupyter notebook.
Please let me know how to work Django in Jupyter notebook or suggest me other options. Thanks a lot in advance.
If by "work" you mean "use Jupyter instead of Django shell", then you can use the following snippet in the first cell:
import sys, os
print('Python %s on %s' % (sys.version, sys.platform))
import django
print('Django %s' % django.get_version())
sys.path.append('<path to your project>')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<your settings module>')
if 'setup' in dir(django):
django.setup()
I am also adding /Users/soon/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4445.76/PyCharm.app/Contents/helpers to the sys.path but unfortunately I can't remember why.
If you want to develop the website using Django, I'm afraid you can't. Use good IDE - e.g. PyCharm Pro

import error using djangorestframework with django-nonrel

I'm working on a Django-nonrel project using Django-rest-framework and I've run into the following error after following the instructions in the Token Authorization section of their Authorization API Guide:
ImportError: No module named rest_framework.authtoken
Normally I would just assume that this is something to do with my PYTHONPATH but I don't think that's exactly what the problem is because I can import this framework from both IDLE and the project's shell run using manage.py, the latter both with and without a virtual environment. Within the virtual environment I have installed django and djangorestframework, which should be the only requiremnets for the project thus far.
I had thought that it might have been a Python versioning problem but given that I can import the package directly from the project's shell (running under the virtual env) I'm kind of at a loss since theoretically running manage.py from the same virtual environment should result in the same context for locating libraries.
I've spent a long time searching around trying to fix this issue but to no avail. Any suggestions? I'm happy to provide any additional information as needed!
A few things to check:
Are you sure you are using the directory one level above rest_framework in your PYTHONPATH?
Did you restart your shell after installation?
Take a look at VirtualEnv PYTHONPATH setup. Did you try setting the PYTHONPATH for virtualenv explicitly?

Installing, configuring and developing for django on ubuntu 10.10 server

I wanted to start getting into developing with Django, however, I am unable to figure out how to make it work. I have installed apache2, I have tried many tutorials on configuring apache to run Django, but I just do not understand how it all works together. Can someone give me a dummies guide on how to install it, how things work, and why?
The best way to link between Django and Apache is using WSGI. You will need to install the mod_wsgi apache module to do this.
Next step: modify the apache configuration file to designate where you want the root of your django website.
WSGIScriptAlias / /path/to/mysite/apache/myApp.wsgi
Next, you should create the wsgi file. This is what initializes your django application. An example wsgi file looks like this
import sys
import os
sys.path.insert(0,os.path.normpath(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0,'/path/to/directory/containing/application')
import django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE'] = 'twitmycity.settings'
application = django.core.handlers.wsgi.WSGIHandler()
Once you have this, restart apache
sudo /etc/init.d/apache2 restart
Now, point your browser to the root directory where you established the wsgi handler. This should bring you to the root to your django application. I hope this helps!
Also note, when you make a change to your application, you need to refresh the modified time on the wsgi file to prevent wsgi from just using a cache version of the django application. To do this, execute
touch myApp.wsgi