Hosting a Django project that uses redis on pythonanywhere - django

I developed an e-commerce site on my local laptop and the project uses redis and when I run the project from localhost, it works perfectly using the following
r = redis.StrictRedis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=settings.REDIS_DB):
REDIS_HOST = 'localhost'
REDIS_PORT = 6379
REDIS_DB = 1
Now, I am trying to deploy the project to python anywhere. I have installed the redis and started the server, changed localhost to myhost.pythonanywhere.com but when I run visit the site I get the following error:
Error 111 connecting to 50.19.109.98:6379. Connection refused.
Request Method: GET
Request URL: http://myhost.pythonanywhere.com/en/4/black-garbage-t-shirt/
Django Version: 1.8.3
Exception Type: ConnectionError
Exception Value:
Error 111 connecting to 50.19.109.98:6379. Connection refused.
Exception Location: /home/dguy/dguy/venv/lib/python3.4/site-packages/redis/connection.py in connect, line 436
Python Executable: /usr/local/bin/uwsgi
Python Version: 3.4.3
Python Path:
['/var/www',
'.',
'',
'/var/www',
'/home/dguy/dguy/venv/lib/python3.4',
'/home/dguy/dguy/venv/lib/python3.4/plat-x86_64-linux-gnu',
'/home/dguy/dguy/venv/lib/python3.4/lib-dynload',
'/usr/lib/python3.4',
'/usr/lib/python3.4/plat-x86_64-linux-gnu',
'/home/dguy/dguy/venv/lib/python3.4/site-packages',
'/home/dguy/dguy',
'/home/dguy']
Server time: Wed, 17 Aug 2016 16:10:56 +0100
Can someone please help me. what are the right configuration.

Redis will not work on PythonAnywhere.

Related

ReadOnlyError in Django application with Redis and DjangoCannels

I have a Django app using DgangoChannels, Djangochannelrestframework. It establishes a websocket connection with ReactJS frontend. As channel layers I use Redis like that
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("redis", 6379)],
},
},
}
Redis and Django runs in docker. My redis docker setup is
redis:
image: "redis:7.0.4-alpine"
command: redis-server
ports:
- "6379:6379"
networks:
- nginx_network
When I run my app on production server everything works for 5-8 hours. But after that period, if Django app trying to send a message via ws if falls with the error
ReadOnlyError at /admin/operations/operation/add/
READONLY You can't write against a read only replica.
Request Method: POST
Request URL: http://62.84.123.168/admin/operations/operation/add/
Django Version: 3.2.12
Exception Type: ReadOnlyError
Exception Value:
READONLY You can't write against a read only replica.
Exception Location: /usr/local/lib/python3.8/site-packages/channels_redis/core.py, line 673, in group_send
Python Executable: /usr/local/bin/python
Python Version: 3.8.13
Python Path:
['/opt/code',
'/usr/local/bin',
'/usr/local/lib/python38.zip',
'/usr/local/lib/python3.8',
'/usr/local/lib/python3.8/lib-dynload',
'/usr/local/lib/python3.8/site-packages']
Server time: Tue, 02 Aug 2022 08:23:18 +0300
I understand that it somehow connected with Redis replication, but no idea why if falls after period of time and how to fix it
I have the same error, the possible solution is here
Fix by adding command to docker and disable the replica-read-only config,
add this to your redis docker compose
​ command: redis-server --appendonly yes --replica-read-only no ​
then you could try to verify if the ​replica-read-only​ is disable using​redis-cli > config get replica-read-only​ command , if the result is no then it successful to disable.

django nginx DisallowedHost

i'm using aws ubuntu instance and nginx
i tried to reach my app and i get this error :
DisallowedHost at /
Invalid HTTP_HOST header: '34.227.190.72'. You may need to add '34.227.190.72' to ALLOWED_HOSTS.
Request Method: GET
Request URL: http://34.227.190.72/
Django Version: 4.0.4
Exception Type: DisallowedHost
Exception Value:
Invalid HTTP_HOST header: '34.227.190.72'. You may need to add '34.227.190.72' to ALLOWED_HOSTS.
Exception Location: /home/ubuntu/env/lib/python3.8/site-packages/django/http/request.py, line 149, in get_host
Python Executable: /home/ubuntu/env/bin/python3
Python Version: 3.8.10
Python Path:
['/home/ubuntu/authentication',
'/home/ubuntu/env/bin',
'/usr/lib/python38.zip',
'/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/home/ubuntu/env/lib/python3.8/site-packages']
Server time: Tue, 31 May 2022 10:46:53 +0000
i tried to add my public address to ALLOWED_HOSTS in setting.py file but it doesn't work
my setting.py file :
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['34.227.190.72', 'localhost', '127.0.0.1']

Dockerize Django application with mongoDB

I am working on Django web application which store the data in mongoDB database. When I run the docker using the docker-compose.yml file, it open the login page and gives the CSFR token error. Following are the logs of Django container:
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 61ad29e66ee4fa015775e4b9, topology_type: Single, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('localhost:27017: [Errno 111] Connection refused')>]>
[05/Dec/2021 21:13:23] "GET /dashboard/ HTTP/1.1" 500 94504
Content of docker-compose.yml file:
version: "3.7"
services:
mongodb_container:
image: mongo:latest
volumes:
- mongodb_data_container:/data/db
ports:
- 27017:27017
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- django_data_container:/home/app/webapp
ports:
- "8000:8000"
links:
- mongodb_container
depends_on:
- mongodb_container
Can anyone tell me how I can communicate the Django with mongoDB using dockers?
From the error:
pymongo.errors.ServerSelectionTimeoutError: localhost:27017
we can tell that you are trying to connect to the localhost on port 27017, but localhost will only address the django container itself. In order to connect to another container (mongodb), change the connection string or connection configuration in django settings to point to that other container's name. In your case you want to change localhost to mongodb_container.

Elasticsearch + django: Unknown mimetype, unable to deserialize: text/html

I have browsed for this error on Stackoverflow and elsewhere, but nothing helps.
I have installed django-elasticsearch-dsl and added it to my INSTALLED_APPS. In my settings, I have:
ELASTICSEARCH_DSL={
'default': {
'hosts': 'localhost:9200'
},
}
Next, I have a 'documents.py' file where the logic for Elastic search resides.
When I try running python manage.py search_index --rebuild, I get this error:
elasticsearch.exceptions.SerializationError: Unknown mimetype, unable to deserialize: text/html
I don't know if I understand correctly how to run the ES server. I have tried to run the local server on port 9200 but the issue persists.

Setting up CI/CD on Gitlab for django project

This is the first time I've tried adding CI/CD for a Django project on gitlab. I want to set up automatic testing and deploying to the server in the development branch if it is successful.
With tests almost everything worked out, dependencies are installed and started python manage.py test but the problem with the testing database. The traceback error is a bit lower and here I don't quite understand how the interaction with the database happens during the tests.
Creating test database for alias 'default'...
.....
MySQLdb._exceptions.OperationalError: (2002, "Can't connect to MySQL server on '127.0.0.1' (115)")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
...
super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on '127.0.0.1' (115)")
In the settings of django settings.py, a connector to the database is obtained through such variables in a .env file.
.env
SECRET_KEY=ja-t8ihm#h68rtytii5vw67*o8=o)=tmojpov)9)^$h%9#16v&
DEBUG=True
DB_NAME=db_name
DB_USER=username
DB_PASSWORD=dbpass
DB_HOST=127.0.0.1
And with the deployment of the project, everything is not clear yet. I'd appreciate your help setting it up.
gitlab-ci.yml
stages:
- test
- deploy
test:
stage: test
script:
- apt update -qy
- apt install python3 python3-pip virtualenvwrapper -qy
- virtualenv --python=python3 venv/
- source venv/bin/activate
- pip install -r requirements.txt
- python manage.py test
stage: deploy
script:
...
???
only:
- develop
UPD
Accordingly Ruddra recommendation I added to yml file next line:
services:
- mysql
variables:
# Configure mysql service (https://hub.docker.com/_/mysql/)
MYSQL_DATABASE: test
MYSQL_ROOT_PASSWORD: mysql
connect:
image: mysql
script:
- echo "SELECT 'OK';" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql "$MYSQL_DATABASE"
And as a result I got connect successful status and test error
status with the same traceback as a starting question
Actually you can run MySQL as service in GitLab. For example:
services:
- mysql:latest
variables:
# Configure mysql environment variables (https://hub.docker.com/_/mysql/)
MYSQL_DATABASE: "db_name"
MYSQL_ROOT_PASSWORD: "dbpass"
MYSQL_USER: "username"
MYSQL_PASSWORD: "dbpass"
Update: In your .env file, update the following settings:
DB_HOST=mysql
Update 2: (Based on this issue on GitLab) You can update the code like this:
variables:
MYSQL_DATABASE: "db_name"
MYSQL_ROOT_PASSWORD: "dbpass"
MYSQL_USER: "username"
MYSQL_PASSWORD: "dbpass"
test:
script:
- apt update -qy
- mysql --user=$MYSQL_USER --password=$MYSQL_PASSWORD --database=$MYSQL_DATABASE --host=$MYSQL_HOST --execute="SHOW DATABASES; ALTER USER '$MYSQL_USER'#'%' IDENTIFIED WITH mysql_native_password BY '$MYSQL_PASSWORD'"