Serve multiple Django applications from one server - django

Good morning. I have a dedicated ubuntu server behind my company's firewall. It is using Django, Gunicorn, and Nginx to serve an Intranet application to employees. The original app responds to the URL [server_name]/[original_application_name]. I want to serve additional apps from this server. I have followed this tutorial as I did when setting up the original app.
I can run Gunicorn and serve the app, I have created a second systemd service file that appears steady (copied from the original app with paths changed - service runs), same for a new 'sites-available' file in Nginx (copied from original and modified), new .sock file exists, binding appears successful. However, I have yet to hit on the right configuration combination between settings.py [allowed_hosts], [new_app].service, and nginx etc.
The original app is running and when I try a URL related to the new app it gives an error saying it cannot find the request in the url.py of the original app. The new app would be used by the IT dept. Ideally, the new URL would be something like: it.[server_name]/[new_application_name].
I have looked through other cases with this problem but most use Apache or are on a public hosting site. I have seen this but it requires a "socket file". My original app is not using a socket file. I was hoping to do this without interfering with the original app. Is a "socket file" required? How can I configure this to serve both apps? Never having done this, what will the new URL be? The URL for the admin site in both apps is 'admin/', how can I accommodate this? Thanks!
I combined into one file as you suggested and I am almost there! I have original site responding at [server_name]/inventory and new site responding at [server_name]/assets. Great! My only problem is the admin page. In both apps the admin site is called admin! So, [server_name]/admin brings up the original site. How can I get to the new admin page?

Related

Django, website with changed prefix, how to fix urls globally

I recently deployed my app locally with the help of Apache. I use server name and sufix to get to the content as http://my-server/app. The Problem is every button ignores what the root of my app is. So from my main view at my-server/app I try to redirect to /second_page and it sends me to server/second_page instead of server/app/second_page. Do I have to change every form by hand or is there any way to configure it through settings.py or urls.py?

How do I change my weblate site url

I installed my own weblate server somewhere, then I switched my nginx configuration to a domain name. I went into weblate's django admin, in sites section and updated the site entry to point to the right location.
I couldn't find anything related to this within settings.py. There's a default site id, but it's already set to 1.
After doing this, urls to projects were still using old url.
Update site entry in django admin and Restart the weblate django application. After restarting weblate, the new url from database started working.
There is also this documented section:
https://docs.weblate.org/en/latest/admin/management.html#changesite
Which should refresh website.

Django admin "view on site" gets a wrong redirection

I have just copied a working Django project from a development server to a production server. I have not touched the code in any way. In the admin interface there is a blog app, in each blog post there is a get_absolute_url so the admin creates the "view on site" button. On the development server when I click this button it opens the post on the site as expected: an example link would be /admin/r/25/515/ which when loaded then redirects to /blog_app/posts/515/slug. That's perfect. On the production server though the "view on site" link has the exact same value of /admin/r/25/515/ but when loaded returns a strange url that leads to nowhere: admin/r/25/515/.com/blog_app/posts/515/slug. I have no idea where this ".com" comes from, I started looking everywhere but have no clue. Any idea?
That was an Nginx conf problem, I use it as a proxy / load balancer and in the proxy_pass setting I did not enter the full domain name thinking it was a kind of variable. My bad. Not really Django related but since many people use it that way it can be useful to know.

MemoryError with Django while serving a movie file

In my Django application, I list the contents of a directory which contains movies (of around 400 MB). When I try to play the movie in the browser, I get MemoryError. I have this movie content inside the "media" folder which I have marked to serve as statically.
I believe this movie should have been served directly through my web server without passing the request to Django. Is there some error in my configuration or is there whole together a different solution available for serving movies as in my case.
I am using lighttpd with Django and FCGI.
Thanks.
You are running out of memory because you read the whole file in memory & buffer it before serving it. Remove the static url config from django urls.py and configure that url to be served by lighthttpd.
But the best way for movies of that size are best served is streaming. Take a look at any media streaming server and see if it helps you. This may help you.
Streaming movies by flowplayer and lighthttpd
--Sai
Could it be that you haven't configured lighttpd to handle requests to /media/ itself and Django is running in debug mode (DEBUG = True in your settings.py).
If you follow Django's own docs for lighttpd deployment, this shouldn't happen.
I solved the error myself.
Actually the problem was with a misconfiguration with my lighttpd server. The problem was that I had configured my webserver to redirect every request to Django and allow Django to process the request and server the response through the webserver.
So, what was happening is when I request to play a large movie file (say around 400 MB), this request went to Django and somehow Django was loading the file in the memory.
Since it was an embedded device with a limited memory, Django threw an "MemoryError".
I changed the configuration of my webserver and everything worked like a charm.
Hope this helps someone in the future. Cheers!

Django admin logging out on model save

I'm running a Django app for my root public_html folder, and it's working fine. But there's a problem with one of my other apps. The problematic app is accessed through a redirect to a subdirectory (e.g. http://workingsite.com redirects to public_html, http://brokensite.com redirects to public_html/foo)
The problem is that the session expires whenever anything needs to be saved in the Django admin (either added or changed). If you try again, sometimes it works. This does not happen on my own machine when I run the Django dev server.
The timezone in both of the app settings.py files is the same, which is the same as the timezone in both of the .htaccess files.
The apps are almost identical, except the working app uses WYMEditor and the broken one uses TinyMCE as its text editor. Don't know why that would do anything to cause the problem, but I included it just in case. Also, I've made a custom CSS file for the admin backend in the broken app (again, shouldn't cause a problem).
Seems to have been a configuration issue with the company I was hosting with - it's not happening anymore.