django nginx DisallowedHost - django

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']

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.

Hosting a Django project that uses redis on pythonanywhere

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.

Django: cannot import name resolve_url

I was doing 2nd part of django tutorial when i got this error after going to http://127.0.0.1:8000/admin/:
ImportError at /admin/
cannot import name resolve_url
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Django Version: 1.7.1
Exception Type: ImportError
Exception Value:
cannot import name resolve_url
Exception Location: /usr/local/lib/python2.7/dist-packages/django/contrib/auth/views.py in <module>, line 8
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path:
['/home/xxx/Projects/mysite',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
Server time: Fri, 2 Jan 2015 22:44:47 +0000
I work on:
Ubuntu 14.04.1 LTS 64-bit
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Any idea how it fix?
It is not your mistake , it happened because of improper installation of Django.
My solution is go to the location where django folder is situated.
which is basically
/usr/local/lib/python2.7/dist-packages
From there remove the folder.
sudo rm -rf django
Reinstall Django
sudo pip install Django==1.7
or any version which u want.
Enjoy !!!

Permission Denied IOError

I have a Django application which saves a file in /opt/Webapp/userfiles/but Apache will not permit this.
In my settings file I have:
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'userfiles')
but I have also tried with
MEDIA_ROOT = '/opt/Webapp/userfiles'
The full error is:
IOError at /jobs/new
[Errno 13] Permission denied: u'/opt/Webapp/userfiles/770976f582c9a40152505b9e456bfd2d547ec9e7353c766f1c0e0e9a.py'
Request Method: POST
Request URL: https://10.5.1.133/jobs/new
Django Version: 1.7
Exception Type: IOError
Exception Value:
[Errno 13] Permission denied: u'/opt/Webapp/userfiles/770976f582c9a40152505b9e456bfd2d547ec9e7353c766f1c0e0e9a.py'
Exception Location: /opt/Webapp/jobs/views.py in upload, line 188
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path:
['/opt/Webapp',
'/opt/.virtualenvs/smart/lib/python2.7/site-packages/django_admin_tools-0.5.1-py2.7.egg',
'/opt/.virtualenvs/smart/lib/python27.zip',
'/opt/.virtualenvs/smart/lib/python2.7',
'/opt/.virtualenvs/smart/lib/python2.7/plat-linux2',
'/opt/.virtualenvs/smart/lib/python2.7/lib-tk',
'/opt/.virtualenvs/smart/lib/python2.7/lib-old',
'/opt/.virtualenvs/smart/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7',
'/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk',
'/opt/.virtualenvs/smart/lib/python2.7/site-packages']
Change permissions of directory with chmod and shown commands. Or change your directory. I don't think that /opt is the best place for such files.
https://help.ubuntu.com/community/FilePermissions - Understanding and Using File Permissions

Django 1.6 IOError

I have upgraded my django version from 1.5.2(if i don't remember bad) to 1.6 and now when i execute my project on localhost i get this error:
IOError at /
[Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/httplib2-0.8-py2.7.egg/EGG-INFO/top_level.txt'
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.6.1
Exception Type: IOError
Exception Value:
[Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/httplib2-0.8-py2.7.egg/EGG-INFO/top_level.txt'
Exception Location: /usr/local/lib/python2.7/dist-packages/pkg_resources.py in _get, line 1320
Python Executable: /usr/bin/python
Python Version: 2.7.5
It looks like a Permission Error. Try to change the permissions with the chmod command (http://www.thegeekstuff.com/2010/06/chmod-command-examples/).
For example
sudo chmod -R 755 problematic_folder