How to host deploy my django project to firebase - python

I made this little django project, it shows weather of next three days of given city, its just a single page project, it looks like this
i want to deploy/host it on firebase
my project link here
But i have no idea how to do it, please help.
Edit
Ok, now i know that i can use cloud run for my backend and firebase for my frontend, can someone give me step by step procedure how to put my django files in cloudrun and firebase, and how to connect them, please

Firebase Hosting only hosts static content, which means it doesn't run your Python/Django code. But you can run the code on Cloud Run, and then integrate that with Firebase Hosting. See https://firebase.google.com/docs/hosting/cloud-run

You can upload dynamic or static content on firebase, in the case of a Django app it's dynamic but your stylesheets / scripts are static content.
In settings.py you have to specify a route for your static files and store them in, like : STATIC_ROOT = '/path/to/static'
Then in your server you have to specify that all the static files are stored in the above path.
Find more informations here : https://cloud.google.com/appengine/docs/standard/python3/serving-static-files

Related

Share media between multiple django(VMs) servers

We have deployed a django server (nginx/gunicorn/django) but to scale the server there are multiple instances of same django application running.
Here is the diagram (architecture):
Each blue rectangle is a Virtual Machine.
HAProxy sends all request to example.com/admin to Server 3.other requests are divided between Server 1 and Server 2.(load balance).
Old Problem:
Each machine has a media folder and when admin Uploads something the uploaded media is only on Server 3. (normal users can't upload anything)
We solved this by sending all requests to example.com/media/* to Server 3 and nginx from Server3 serves all static files and media.
Problem right now
We are also using sorl-thumbnail.
When a requests comes for example.com/,sorl-thumbnail tries to access the media file but it doesn't exist on this machine because it's on Server3.
So now all requests to that machine(server 1 or 2) get 404 for that media file.
One solution that comes to mind is to make a shared partition between all 3 machines and use it as media.
Another solution is to sync all media folders after each upload but this solution has problem and that is we have almost 2000 requests per second and sometimes sync might not be fast enough and sorl-thumbnail creates the database record of empty file and 404 happens.
Thanks in advance and sorry for long question.
You should use an object store to save and serve your user uploaded files. django-storages makes the implementation really simple.
If you don’t want to use cloud based AWS S3 or equivalent, you can host your own on-prem S3 compatible object store with minio.
On your current setup I don’t see any easy way to fix where the number of vm s are dynamic depending on load.
If you have deployment automation then maybe try out rsync so that the vm takes care of syncing files with other vms.
Question: What was the problem?
we got 404 on other machines because normal requests (requests asking for a template) would get a 404 not found on thumbnail media.
real problem was with sorl-thumbnail template tags.
Here is what we ended up doing:
In models that needed a thumbnail, we added functions to create that specific thumbnail.
and using a post-save signal in the admin machine called all those functions to make sure all the thumbnails were created after save and the table for sorl-thumbnail is filled.
now in templates instead of calling sorl-thumbnail template tags now we call a function in model.

Can I add permissions to media django media files?

I want to build an app and let user to see some videos just if they have permissions or they paid for that video. I am using Django and I want to add ngnix and gunicorn to serve media files.
I am not sure if once the user has the url of the video, how can I block him to not see the video if his payment expired or he doesn't have the permissions. For now I let django to serve the videos and I overwrite the server method and if he doesn't have access to video I return 404.
You need to implement the so-called 'X-Sendfile feature'. Let's say your paid-for files will be served from location /protected/ - you need to add to nginx's config:
location /protected/ {
internal;
root /some/path;
}
then when you want to serve your user a file named mycoolflix.mp4 your app needs to add header X-Accel-Redirect: /protected/mycoolflix.mp4 and the file /some/path/protected/mycoolflix.mp4 will be served to the user. More information in the nginx documentation here and here.
Serving files from your views is not a good idea - it makes one of your Django processes busy until the download is complete, preventing it from serving other requests.

How to configure django app (Heroku) as subdomain of static portfolio site (Dreamhost)

I have a simple portfolio website with some html and css files in the root directory of the site hosted by Dreamhost. I also have a Django app that I'd like to place in a subdomain of this same website. However, Heroku will be serving the django app. I'm confused about how to organize and configure the whole portfolio/django website. How would the system work using two different hosts? Should I integrate the static portfolio site into the django project? Or do I keep them completely seperate and have them live on their own servers? Sorry if my question doesn't make sense. I'm very confused.
As far as the internet's concerned, a subdomain is a completely separate website. You can point a subdomain at whatever address you like; the internet doesn't care that it's a completely separate host. You can host your system however you like: both on Dreamhost, both on Heroku, or one on each. The latter setup is the most complex, so we'll walk through that one here.
Let's say your site is example.com and you want the portfolio site to be portfolio.example.com. If your app's running on Heroku, it'll have a name similar to yourportfolio.herokuapp.com. So we need to do two things: tell Heroku that your app is served from portfolio.example.com, and tell the DNS system to point from your subdomain from Heroku.
Pointing the subdomain to Heroku
Presuming your domain name is hosted on Dreamhost, go to the Domains section of the control panel, then Manage Domains. Under example.com is a link called DNS. You need to add a custom CNAME record; set name to portfolio, type to CNAME, and value to yourportfolio.herokuapp.com.. CNAMEs are a way of setting up aliases on the web; they mean "this site is also known as foo".
Telling Heroku to serve your app
Within your Heroku project, run heroku domains:add portfolio.example.com.
Heroku has documentation about subdomains here, which is a useful overview of the process as well as giving details of more complex setups.

Serving static html pages with flask on heroku

Im trying to figure out how to serve static pages from heroku with a flask app. I found this with some searching:
#app.route('/foo/<path:filename>')
def send_foo(filename):
return send_from_directory('/path/to/static/files', filename)
But, this would be quite inefficient. Is there a way to have the front facing server directly serve these files?
Normally you would do this with mod_rewrite with apache or similar, but afaik heroku doesn't let's you change the http server configuration.
You'll need to use the rack middleware, it lets you write url rewrite rules with ruby. (check this out : http://icelab.com.au/articles/useful-heroku-friendly-rewrites-with-rack-rewrite/ )

Passing around urls between applications in the same project

I am trying to mock-up an API and am using separate apps within Django to represent different web services. I would like App A to take in a link that corresponds to App B and parse the json response.
Is there a way to dynamically construct the url to App B so that I can test the code in development and not change to much before going into production? The problem is that I can't use localhost as part of a link.
I am currently using urllib, but eventually I would like to do something less hacky and better fitting with the web services REST paradigm.
You could do something like
if settings.DEBUG:
other = "localhost"
else:
other = "somehost"
and use other to build the external URL. Generally you code in DEBUG mode and deploy in non-DEBUG mode. settings.DEBUG is a 'standard' Django thing.
By "separate apps within Django" do you mean separate applications with a common settings? That is to say, two applications within the same Django site (or project)?
If so, the {% url %} tag will generate a proper absolute URL to any of the apps listed in the settings file.
If there are separate Django servers with separate settings, you have the standard internet problem of URI design. Your URI's can be consistent with only the hostname changing.
- http://localhost/some/path - development
- http://123.45.67.78/some/path - someone's laptop who's running a server for testing
- http://qa.mysite.com/some/path - QA
- http://www.mysite.com/some/path - production
You never need to provide the host information, so all of your links are <A HREF="/some/path/">.
This, generally, works out the best. You have can someone's random laptop being a test server; you can get the IP address using ifconfig.

Categories

Resources