Is there any well-known reason for this Django issue? - django

I created a django (1.8+) project some time ago, and it's weird that it works well with runserver, but it's not working with gunicorn (requests never end, nothing is received using curl or any browser to test my server). The funny fact is that it worked until recently (I'm in an AWS/AmazonLinux server; never changed the server image).
Since this timeout really annoyed me (and still does) I checked the project, opened a python interactive interpreter inside my virtualenv, and tried:
from my_project.wsgi import application
inside the appropiate folder (so this statement does not raise ImportError). But that single line took a lot (at least 10 seconds) to execute.
I think that is the reason why is gunicorn not working.
Is there any well-known reason this could happen? How can I debug further?
Edit My gunicorn command: gunicorn trescloud_landing.wsgi:application --daemon --bind=127.0.0.1:8888 --pid=/opt/webapps/pid/landing-pages.pid --access-logfile=/opt/webapps/log/landing-pages.access.log --error-logfile=/opt/webapps/log/landing-pages.error.log
Edit 2 Changed --timeout=600 (yes, 10 minutes) and gunicorn worked. But it is still troublesome to me that the server has a somehow low wsgi bootstrap

Related

Port doesnt respond

After python manage.py runserver , all goes well,when I use the link http://127.0.0.1:8000/ generated it fails to open the Django project on windows 10 completely, I can't figure out the problem. I've tried disabling proxies and firewalls and antivirus but still unable to open.Also tried restarting the computer and entire procedure,spent hours on this, what could be the problem

Django dev server extremely slow

We have an app that works perfectly fine on production but very slow on the dev machine.
Django==2.2.4
I'm using Ubuntu 20.04 but other devs are using macOS and even Windows.
Our production server is a very small compared to the dev laptops (it works very slow on every dev environment, we are 5 developers).
The app makes several request since it's a Single Page application that uses Django Rest Framework and React.js in the front-end.
We have tried different databases locally (currently postgresql, tried MySQL and sqlite3), using docker, no docker, but it does not change the performance.
Each individual request takes a few seconds to execute, but when they go all toghether the thing gets very slow. As more request are executed, the performance starts to drop.
It takes the app between 2/3 minutes to load in the dev environment and in any production or staging environment it takes little above 10 seconds.
Also tried disabling DEBUG in the back and front-end, nothing changes.
It is my opinion that one of the causes is that the dev server is single thread and it does not process a request until the previous is finished.
This makes the dev environemnt very hard to work with.
I've seen alternatives (plugins) to make the dev server multi-thread but those solutions do not work with the latests versions of django.
What alternatives could we try to improve this?
Looks like posting this question helped me think in an alternative. Using gunicorn in the dev environment really helps.
Installed it with
pip install gunicorn
And then execute it using this:
venv/bin/gunicorn be-app.wsgi --access-logfile - --workers 2 --bind localhost:8000
Of course if I want to access the static and media files I'll have to set up a local nginx but it's not a big deal

How do you keep a django digital ocean droplet live at all times?

If I purchase a droplet from digital ocean and install Django on it and get a basic hello world webpage going, how could I run this server and keep it live without a terminal staying open 24/7?
For example, if the droplet I.P. Address was 162.243.250.17:8001, and I entered this code:
python manage.py runserver 162.243.250.17:8001
I'll get the output:
Validating models...
0 errors found
January 31, 2014 - 22:58:23
Django version 1.6.1, using settings 'django_test.settings'
Starting development server at http://162.243.250.17:8001/
Quit the server with CONTROL-C.
But so how do I keep this going even if I close my terminal? I'm going to need a website to stay live.
UPDATE I found the solution
This is the first step if your production server/droplet is running Linux
in order for your website to go live as if it were production ready, you need to install nginx like this:
sudo apt-get install nginx
finally, get your home page to go live with a "Welcome to nginx!" like home page by entering this in your terminal:
sudo service nginx start
That's it!
The next step would be to make it so that the Django development server's address to replace the "Welcome to nginx!" page. I'm going to start working on that now, I'll keep this thread updated with the solution.
This is not the way to run django based webserver. You need to use apache/nginx.
If you use nginx you can combine it with wsgi container like gunicorn and supervisord.
For example, look here http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/ or search for these terms.
What eran said is absolutely right: you would never use the built in django server for anything in production, and use nginx or gunicorn/uwsgi.
However, you seem to be learning Django at this point. So, even if you just close the terminal (if you are SSH-ing) or close the browser, the program should run as long as you do not press ctrl + c, or kill the running server. However, you will find out that it crashes a lot and is very slow.
Learning nginx and uwsgi is too much work at this point. Learn django properly on your laptop first, and when you feel comfortable enough, then maybe deploy on digital ocean. Why spend money, albeit it is pretty cheap at $5.

django-rosetta not saving translations on hitting "Save and Translate next block" in Chrome

django-rosetta is not saving the translations on hitting "Save and Translate next block" in Chrome.
It seems to be working fine in Firefox though. I tried clearing cache so that does not seem to be an issue.
On Chrome, after entering translated text and hitting the "Save.." button, I'm redirected to the main screen without my changes being saved.
I updated to the most recent version of rosetta thinking that the Issue13 fix would solve my problem but it does not.
I've also made sure that my .po and .mo files have 777 permissions.
Been a long time, not sure if you were able to sort it out?
I have a dev environment running Django 1.4.2 and Rosetta 0.6.8 on Lion and it works flawlessly. Im setting it up with the python manage.py runserver command.
Then when I wanted to deploy it to my Ubuntu 12 server Rosetta started to behave quite weird. I installed Nginx as a proxy and was running apache with wsgi. Same versions besides that. It was not just the translated strings that were not being saved but the navigation became absolute random. Did not matter what button I pressed, I suspected the proxy configuration wasn´t setup properly since I could run it locally in my dev.
To make a long story short, I finally shutdown nginx and apache and just ran django with the builtin and hence got the django logs in my terminal. It got a lot of errors that made the server restart itself every few seconds. This obviously explained the somewhat non-reliable behavior of the app. I dumped the database in production and resynced it. Now when I runserver in production it works just as good as in my dev. When I plug in apache to do the job (going around nginx for the moment) the problem still persist, not as bad as before though. Browser does not make any difference.
One of the errors that did pop up was this
Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x1781710>>
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 50, inapport_excepthook
if not enabled():
TypeError: 'NoneType' object is not callable
Still havent solved this but will be posting the solution when I do.
EDIT:
Scratched the good ol´ apache wsgi setup and went for Gunicorn instead. Works really well now, not sure what that was all about but I´m quite happy with the new setup anyways.
Here´s a good setup guide
http://ijcdigital.com/blog/django-gunicorn-and-nginx-setup/
hopefully this helps someone in the future!

bitnami django, solution to restarting service?

Hey I use installed bitnami django 1.3.0,
but whenever I add changes to urls.py or views.py in my system due to some error. The error won't disappear after refresh.
I have to restart my bitnami Service, "stop" and then "start" it, which is time consuming, I feel like I'm coding C# apps in visual studio. Sometimes even that doesn't work, I have to sometimes restart my computer and then I suddenly realize "oh wow, the error is solved now!"
Any solution to this? Why does everything require a runserver / restart?
You can use Apache for deploy your application in production but use the Django server for development. You will need to configure your application for being served by apache later (modifying the settings.py and the apache configuration file) but during the development you won't need to restart the server for every change.
Everything requires a restart because of the way that the python process operates. It does not reload the file when it's changed (outside of runserver..which is an anomaly, and just there for convenience)
Python execution isn't like PHP execution in that way, and your code isn't dynamically loaded on every page refresh, it's loaded on every server restart.
Hope that helps.