Issues with poetry when deploying Django app to Render - django

I am following the “Getting Started With Django on Render” tutorial (https://render.com/docs/deploy-django#update-your-app-for-render) and all was going well until I got to “Configure Django for PostgreSQL”. I have everything copied correctly but I am getting errors now when I run python manage.py runserver this is the error:
File "/Users/user/Desktop/First_Program/Portfolio_Projects/Charter_Django_App/Charter_Django_App/config/settings.py", line 14, in <module>
import dj_database_url
ModuleNotFoundError: No module named 'dj_database_url'
even though when I run poetry show it clearly says it is installed
asgiref 3.5.2 ASGI specs, helper code, and adapters
brotli 1.0.9 Python bindings for the Brotli compression library
dj-database-url 1.0.0 Use Database URLs in your Django Application.
dj-email-url 1.0.6 Use an URL to configure email backend settings in your Django Application.
django 3.2.16 A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
django-cache-url 3.4.2 Use Cache URLs in your Django application.
environs 9.5.0 simplified environment variable parsing
gunicorn 20.1.0 WSGI HTTP Server for UNIX
marshmallow 3.19.0 A lightweight library for converting complex datatypes to and from native Python datatypes.
packaging 22.0 Core utilities for Python packages
psycopg2-binary 2.9.5 psycopg2 - Python-PostgreSQL Database Adapter
python-dotenv 0.21.0 Read key-value pairs from a .env file and set them as environment variables
pytz 2022.6 World timezone definitions, modern and historical
setuptools 65.6.3 Easily download, build, install, upgrade, and uninstall Python packages
sqlparse 0.4.3 A non-validating SQL parser.
whitenoise 6.2.0 Radically simplified static file serving for WSGI applications
And also shows in poetry.lock file.
If anybody knows why this is happening and can please give me some insight. I followed the tutorial to the T but am new to using poetry.
I am also a bit confused as to where I store the environment variables such as SECRET_KEY as the tutorial is not very descriptive at that point. I am used to using pip install environs[django] then running
env = Env() env.read_env()
And reading from the .env file
Thank you x1000 in advance to anybody that may help me get my application running

I'm guessing the error is from you're virtualenv either you're not activating it or the packages are not installed in the virtualenv.
if you are using vscode try Ctrl + Shift + p and search for Python: select interpreter make sure the interpreter vscode is using is you're venv if click on it and go to myvenv/Scripts/ there you can see a file python.exe choose it.

It's hard to be sure as you didn't post that many things to begin with. But if you managed your virtual environment through poetry, then you should run :
poetry run python manage.py runserver
(instead of python manage.py runserver)
(As for environment variables, I've been advised to try python-dotenv. I haven't had the chance to test it yet, but it seems easier than most things. I'm not sure if that will help you with render.yaml though...)

Related

Django installation dependency errors after rolling back to Python 3.7.6

I'm working my way through the tutorials in William S. Vincent's Django for Beginners. Everything worked fine until Chapter 4 (the Message Board app), when like a nOOb I ran into the known bug that causes Django's web server to crash when attempting to serve /admin/ using Python 3.7.* in pipenv. So, I upgraded Python to 3.8.2, and it still didn't work. So I foolishly deleted 3.8.2 and rolled back to 3.7.6, and now I can't install Django at all with pipenv due to a variety of pipenv.exceptions.ResolutionFailure statements.
I've reinstalled Python 3.7.6 AND 3.8.2, pip3, pipenv and Django (through Anaconda as well as outside of it), but I'm still getting the same litany of errors that won't let Django be installed in my virtual environment. Using pipenv lock --pre --clear doesn't fix it.
The telling line is "ERROR: No matching distribution found for django-3-0"
Does anyone have any idea how to resolve this or what packages I need to uninstall/reinstall?
Thanks much! :)
Manually delete the whole pipenv cache directory,
~/.cache/pipenv - (Linux)
%LOCALAPPDATA%\pipenv\pipenv\Cache - (Windows)
Also delete the previous virtualenvs which were created within the .virtualenvs folder. (or the entire folder)
Default location: C:\Users\{username}\.virtualenvs
Also delete Pipfile.lock file and then recreate your virtual environment:
pipenv shell
Well, good news! On a hunch, and to rule-out any weirdness, I tried recreating a clean Django install by using virtualenv, since pipenv is giving me problems. It worked! I must have borked something with pipenv when I removed/re-added/re-removed Python 8.x. Thanks for your inspiration for this workaround, #Magicoder!

Django site on Webfaction - how the flow of information works, and integrating with Pycharm remote dev

I have been learning Django in the development mode for a two months and I am up to speed with most basic aspects of python + django now. However, I was using the built-in runserver till this time
Now, I have got a Webfaction hosting account and wanted to know the following
1) Webfaction sets up the project with a certain Django version (say 1.6.4) and Python version (say 2.7) initially
The project directory (for say project MYAPP) is /<>/webapps/MYAPP
When the site is running in production mode, how does the apache server know which Python version, and which site-packages versions to use with the MYAPP source code to render the site?
I can see that the MYAPP folder has a lib/python2.7 folder, however when I am connected to SSH terminal, and do a "which python", i see :
which python
/usr/local/bin/python
so, do I take that this is the Python executable that is being used for rendering the website instead of the one in webapps/MYSITE/lib/python2.7 folder? How does the information/data flow about which programs to use during rendering the site with apache mod_wsgi work?
2) I was using a virtualenv in the development mode during testing. How do I use this on webfaction in production mode?
3) I am using Pycharm IDE. It worked well for the development mode. I can see that it has a remote interpreter configuration and a Deployment setting/option.
The python path that the remote interpreter settings tool detects automatically is the python executable at /usr/local/bin/python
Is this fine, or should I be pointing it to the more local python2.7 in the webapps/lib folder?
Thanks a lot of the answers and pls let me know if you need any supplemental info
Note to the OP: This should really be three separate questions.
1) For WebFaction, your Django app will use the Python version (and libraries, etc.) defined in:
~/webapps/<appname>/apache2/conf/httpd.conf
Specifically, you'll want to look at what is defined for WSGIPythonPath (which should mostly match up with WSGIDaemonProcess unless you modify the config and are doing something strange).
Note that which python just tells you what the default python is in your shell. This has nothing to do with the config file for the webapp.
2) You may want to expand on this as to exactly what your use case is and why the default Django webapp created by WebFaction doesn't fit your needs. But the short answer is:
Create a virtualenv on your WebFaction account.
Install Django, etc. into the virtualenv.
Edit the httpd.conf file I mentioned above to use your virtualenv instead.
I've done this with both a Django webapp made via the WebFaction control panel and via a custom mod_wsgi webapp. So it does work. Just make sure to use the right Python version when making your virtualenv.
3) I don't use PyCharm so I can't answer this (one reason why this question should be split up).

How can I get started developing with Django on Cloud9?

So Cloud9 looks really cool, and you can create python files in it, but I can't figure out how to get it to run a Django project. I imported one from my github account into the IDE, but it says I need to install Django (a django.core import error) so I need to help getting going. Any Ideas? Any skeleton projects on the web that I missed that will do this?
After some research and looking for an answer on different blogs and sites I finally found a solution:
Create a new workspace
Execute easy_install django to install Django
Create new Django project python ./../bin/django-admin.py startproject myproject
Start dev server python ./myproject/manage.py runserver $IP:$PORT
Access http://projectname.username.c9.io
Voilà!!!
I hope this helps
Just write commands in shell as it is:
sudo pip install -r requirements.txt (you should have there django)
than
python manage.py runserver $IP:$PORT
you will have app on https://projectname-username.c9.io
You may also have to add the proper path: <workspace_name>-<username>.c9users.io to the ALLOWED_HOSTS list in django_project/settings.py file, as suggested by the error message returned by django server. Tested on Django 2.0

Creating an app on Heroku with Django and NPM

I'm writing a Django app that includes some CoffeeScript in it. To allow for this I'm using django-compressor which compiles the CoffeeScript to JS before the app is launched. django-compressor requires that NPM is installed on the machine to compile the CoffeeScript.
Now I want to deploy this app on Heroku. I can't put npm in my requirements.txt so I am wondering how I can get npm on the Heroku server?
If you want to avoid maintaining a custom buildpack, you can use the multi buildpack.
Using the multi buildpack is super simple:
Run heroku config:add BUILDPACK_URL=https://github.com/ddollar/heroku-buildpack-multi.git
Create a .buildpacks file in the root of your repository with two lines:
https://github.com/heroku/heroku-buildpack-nodejs.git
https://github.com/heroku/heroku-buildpack-python.git
Create a package.json file with your npm dependencies.
Run npm install
Note: The multi buildpack is a much nicer way to accomplish this these days :)
I've created a fork of the official Python heroku buildpack that allows an optional npm_requirements.txt for installing such dependencies.
I am now using coffeescript and less-css with django-compressor on heroku :)
https://github.com/jiaaro/heroku-buildpack-django
Edit: To switch to my buildback from the standard buildpack:
use the heroku command line app to set the BUILDPACK_URL environment variable:
heroku config:add BUILDPACK_URL=git://github.com/jiaaro/heroku-buildpack-django.git
You can create your own buildpack, that mix nodejs buildbpack and python buildpack. Or compile your CoffeeScript on your machine and put it on S3.
I found this question in Google while solving the same problem for myself.
I merged two official buildpacks (python and nodejs), so now one can have Django project with standard npm-description file package.json by running this command:
heroku config:add BUILDPACK_URL=https://github.com/podshumok/heroku-buildpack-python
This solution differs from Jiaaro's one in the following:
it is based on the newer (dec 12) versions of buildpacks (for example, it runs collectstatic on deployment)
you need correct package.json file (at least name and version of your product should be specified in this file)
npm dependencies should be listed in package.json
#Jiaaro 's solution didn't work for me... Causes some weird error... /:
File "almalinks/manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
Was too tired to deal with it, so I looked around and I found this nifty resource:
- The heroku-django cookbook
They explain how you can add your own scripts that hook into heroku's default buildpacks.
Worked like a charm. :)
Things have changed in Heroku land
There is no need for multi build packs, .builpack files, or custom build packs. Simply add the required official heroku build packs to your heroku app and they will execute in the order entered. Use the index option to reorder them as required.
heroku buildpacks:add --index 1 heroku/nodejs -a your_app_name
There is also no need for, gunt tasks, apps like django-bower, or other specialized tools that take up server resources and slow build time.
You can check out my tutorial on how to seamlessly integrate Django + Bower + Heroku here.

Django application installation

I'm still busy with my Django learning adventure. In another post I asked about how to structure a Django project and applications using buildout. In the details of doing this arose another issue, simply installing 3rd party Django applications using either easy_install or setup.py. My question is, where should you install a Django application? If looking at Django documentation, one would think to put a Django application inside the project folder. But if your Django application is an egg (a mystifying term in my opinion) and you use easy_install without option '-b' (build-directory) the application will be installed into your current python site-packages directory. Using option '-b' will put a copy of the application in your directory, but still will install it in your current site-packages directory. Then there are other options like --install-dir and prefix. Also how should installation happen when using setup.py which have similar options as buid-directory, install-dir, and prefix?
Is there a 'good practice' standard for installing 3rd party Django applications into a Django project?
Thank a lot,
Todd
They usually aren't installed directly into the project. They're either installed into the system's site-packages/ directory, or in the virtualenv's site-packages/ directory, or in some other well-defined place that the sysadmin has set for this purpose.
This is where virtualenv comes into its own. It basically enables a project-specific site_packages directory, where you can install all the third-party applications that relate to your project. I'd definitely recommend it.
Follow these steps :
change the path according to your local setup
C:\Python27\Lib\site-packages>python pip install django
Create Project
Go to folder where you want to create a project
E:\djangoProject>C:\Python27\Lib\site-packages\django\bin\django-admin.py startproject myproject
python manage.py help is used to list all the commands
Manage.py  This file is kind of your project local django-admin for interacting with your project via command line (start the development server, sync db...)
Run Server
E:\djangoProject\myproject>python manage.py runserver
Create App
E:\djangoProject\myproject>python manage.py startapp myapp
Go to myproject  settings.py and register your app “myapp” created under INSTALLED_APPS
Migrate DB  E:\djangoProject\myproject>python manage.py migrate
Migrate will create necessary tables or collections depending on your db type, necessary for the admin interface to run