how to handle this syntax error in django project - django

I have transferred my Django project onto Linode server already. The project was developed in VS, within anaconda (base) environment on my Mac.
After I downloaded it onto the Linode server when activating it, it gave me too many dependencies (not all needed it seemed). One of them prevented me from completing the process.
So, I sudo installed (in venv) needed packages (within the project’s virtual environment), for example:
certifi,
chardet,
Django…,
After creating ’static’ in the settings.py, on the linode server, I tried to collect static :
~$ python manage.py collectstatic
BUT I am getting this Error message:
/home/..../django/users/models.py", line 16
return f'{self.user.username} Profile'
^
SyntaxError: invalid syntax
What is wrong here? How can I fix this error?

Check your python version. f strings are a fairly new feature, introduced with python 3.6. It's likely that you run an older version.

Related

Issues with poetry when deploying Django app to Render

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...)

/opt/alt/python39/bin/lswsgi: No such file or directory

I have a shared Cpanel host with the Litespeed web server. I want to deploy a Django application on it. After creating a Python application inside the Cpanel where I have not deployed the application on the host I try loading the website, and instead of displaying the Django version, I face 503 Unavailable!!
Also inside the "stderr.log" file, there is the following error.
/usr/local/lsws/fcgi-bin/lswsgi_wrapper: line 9: /opt/alt/python39/bin/lswsgi: No such file or directory
I'm creating the application with Python 3.9.
But it works when I create it with Python 3.8 and show the following message when I load the web,
It works!
Python 3.8.6
The issue is mostly caused by the lack of the Python 3.9 WSGI package. On out-of-date versions of LiteSpeed, the package needs to be installed manually.
To work around this, first ensure that LiteSpeed is up to date. LiteSpeed must be at version 5.4.10 for this to work. Once that is confirmed, execute the following script from LiteSpeed. It will pull the required Python Selector packages:
/usr/local/lsws/admin/misc/enable_ruby_python_selector.sh
Refer cpanel support

The FastCGI process exited unexpectedly - (Django iis hosting)

I am following this tutorial to hosting my django application on windows IIS Manager.
After following all steps from the tutorial I have got the following HTTP Error 500.0 - Internal Server Error
Is there any way to solve the issue?? I didn't find any solution for this...
I am using,
Python==3.10.0
Django==3.2.8
wfastcgi==3.0.0
IIS 10.0.17763.1
I had this same issue (Error code 0x00000067) and have been beating my head against a wall for 24 hours. I am new to Windows servers, but have been deploying Django Apps on Linux servers for over a decade.
In my case, I had deviated from the Johnnyboycurtis tutorial you referenced in a few ways:
I tried to install every python package in a virtual environment
I did not install Python at C:\, but rather hidden under my user's AppData\local\Programs\Python dir (the default)
I am assuming that your app works when running the Django Development server (py manage.py runserver -> http://localhost:8000 ) your app looks as expected (when DEBUG = True in settings.py)
Fixing my code install meant taking the following steps:
Navigating to C:\Users\MyUserName\AppData\local\Programs\Python, and following Johnnyboycurtis' instructions (but on Python310 instead of Python37) I added permissions for my AppPool to run Python.
I had to install wfastcgi OUTSIDE of my virtual environment:
deactivate (if you have a python virtual environment activated)
py -m pip install wfastcgi
Note that I did NOT have to change any other settings that were assuming my virtual environment:
web.config: 'scriptProcessor' still points to a python.exe and wfastcgi.py inside my virtual environment
IIS FastCGI Settings 'Full Path' and 'Arguments' both refer to files in my virtual environment

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!

Fatal Python error: PyEval_AcquireThread

I just put up a django application online and i'm getting this error in my error log
Fatal Python error: PyEval_AcquireThread: NULL new thread state
I have seen a few similar questions online, but they do not explain this error.
The problem can appear if apache mod_wsgi and mod_python are both enabled.
Try
sudo a2dismod python
sudo service apache2 restart
This tends to indicate that your mod_wsgi is compiled for a different Python version and/or a different Python installation than the Python virtual environment or Python installation it is trying to use at runtime.
In other words, you are mixing compiled shared library and extension modules from different Python installations. This can cause all sorts of problems.
Go through the checks in:
http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation
to understand how your mod_wsgi has been built and verify it is what you expect it to be.
In my case it was permissions for the directories where Django or Flask project are. They were created from root, so I changed them to www-data in my case.