Usage of Heroku with Django - python

I'm currently learning Django and getting a decent grasp of it. I know you can start the server with python manage.py runserver. But then there is also heroku ps:scale web=1 followed by heroku open to open the web page at the subdomain you chose on the host herokuapp.com. What is the point of using heroku vs just runserver? When it comes time to deploy to production is that what heroku is used for as I read it was for deployment? What is exactly is heroku used for and what are the reasons? When creating a professional django web site/application is heroku required or just very useful? If so why? I don't really understand the reason for using heroku. And I'm curious what the best practices are for django and whether they include using or not using heroku.
Aside from the heroku question, what are the best practices for django as far as setting a domain name and such (other than the herokuapp.com subdomain name) and also as far as what other components/software solutions are worth using with django?
One of the things I'm working on is an FTP portal on the website that people can login to and upload files. Does django have plugins/python modules for such a thing or must it be done custom? Which brings me to my final question, What kind of addons/modules does django offer to avoid reinventing the wheel when implementing a django website/app's certain features?
The tutorial I'm following: http://tutorial.djangogirls.org/en/django_orm/README.html
has helped me get a grasp on django, but I'm curious about the more advanced parts of it. Such as custom modules, custom styling, custom scripts (whether they are js or strictly python).
If someone could point me in the right direction or offer any suggestions it would be much appreciated. The tutorial has me using the modules: dj-database-url, whitenoise, gunicorn, python-dateutil, psycopg2 , requests. I know what some of them do, but what is the purpose of psycopg2, whitenoise, dateutil, and dj-database,url? Are there any other modules worth using?
Sorry for the wall of questions, but I'm trying to understand django as best I can so that I can do a correct assessment when putting together a django project.
Thanks in advance.
EDIT, final questions:
1) Also when i used heroku they gave me a subdomain at herokuapps.com Can I set my own URL and still use heroku?
2) And last but not least, heroku makes you register and gives you a subdomain on herokuapp.com Is there a way to use heroku with your own domain rather than the one they give you? Is there a specific way to do it?
EDIT 2:
When I do runserver it works fine, but when i run it with heroku ps:scale web=1 and then heroku open it opens but only shows the title at the top of the page, and no content. Not sure exactly why that is. Can someone explain the difference between runserver and heroku ps:scale web=1 followed by heroku open??

python manage.py runserver is only for development. Never use it in production.
Heroku makes it super easy to deploy your Django code. You only need some configs and git push heroku master. But it's not the only way to deploy your code. You can also set up a server with a webserver like Nginx, Gunicorn as WSGI server, and a database of your choice. To set things up, you need to know something about Linux and the command line.
So if you don't know how to set up a server, Heroku can be very useful.
Heroku supports custom domains, and it's easy to set up.
As Mikeec3 mentioned, "Two Scoops of Django" is a very good read.
If you are looking for django packages, www.djangopackages.com is a good place to start.

psycopg2 is for postgresql usage with your app. White noise allows you to serve your staticfiles without nginx if using gunicorn. Requests is an easy to use urllib2 library with pythonic calls to work with apis from other websites.
If you want to do some testing with say facebook logins Heroku is a great place to go to, especially if you need a url outside of localhost.
My suggestion is you read Two scoops of Django http://twoscoopspress.org/products/two-scoops-of-django-1-6
It goes over alot of best practices and serves as a great tutorial after polls. I suggest also incorporating django-bower into your project. Ill list a few great things to add.
Django Bower: https://github.com/nvbn/django-bower - Helps organize static files
Crispy Forms : https://github.com/maraujop/django-crispy-forms - Great form rendering, great with bootstrap
Django Rest Framework: https://github.com/tomchristie/django-rest-framework - REST APIs
These are just to start, there's alot to learn.

Related

does it make sense to use apache as web server for django in development mode

I'm a newbie in django and have a project that involves distributed remote storage and I'm suggested to use mod x-sendfile as part of the project procedure.
In have a django app that receives a file and transforms it into N segments each to be stored on a distinct server. Those servers having a django app receiving and storing the segments.
But since mod x-sendfile works need apache and I am just in developing and trying stage this question occurred to me.
I googled a lot but found nothing in that regard.
So my question being: Is it possible to use apache as django web server during the development of django apps? Does it make sense in development mode to replace apache with django built-in web server?
There should be nothing stopping you from installing a copy of Apache on your workstation and using it for developing, and since you're working on something that depends on some of that functionality it makes perfect sense for you to use that for your development server instead of ./manage.py runserver.
Most people use Djangos built-in server because they don't need more than that for what they're trying to do - it sounds like your solution does.
Heck, since you're testing distributed you may even want to consider grabbing a virtualization tool (qemu, virtualbox, et al) so you can have a faux-distributed setup to work with (I'd suggest doing a bit of scripting to make it easy to deploy / restart them all at once though - it'll save you from having to track down issues where the code that's running is older than you thought it was).
Your development environment can be what you need it to be for what you're doing.

Deploying Python on AWS Elastic Beanstalk

I'm coming from a php background and now I'd like to host a Python web application on AWS but have a few questions about it.
Do I need to use a framework like Django/Flask? Is there anyway I can use core Python to do the same? Back in php, although I tried learning Laravel in the middle, I was able to just use core php with apache and host up my website.
Tried deploying my Python code on my AWS but got an error that application.py was missing. I'd like to know, what exactly is application.py and what is it supposed to contain? Similarly, what's wsgi.py I see here and there. Are these actual files I need to create for the web app to be hosted? Is there some specific code that has to go into them or are the pre-created files by frameworks like Django, etc? Because I could hardly find too much information on them online.
I had recently tried following this tutorial from the AWS official site but to no luck.
The reason for being reluctant to use Django is the shortage of time to learn it. But if it were to make the task of hosting a Python web app easier, I would definitely look at it.
And how is the version of Python set? Because the Python codes I've written use the python3 libraries for BeautifulSoup and urllib.
I have read a lot of articles on the web but the first thing I get on searching for Python on the web or with AWS, is Django or Flask or something. How exactly does it work? When it came to php, it was simple copying the files into the /www/ folder of the server machine and I could access the website via it's url. Maybe I've read too many posts to put them all together so could someone please set it straight for me? It would be greatly appreciated!
Thanks a lot!
You will waste a lot more time trying to write a WSGI application yourself from scratch. Use a framework, it will save you a lot of time.
PHP is very different to WSGI and WSGI sits well below the level of functionality that PHP provides out of the box. PHP is more like what frameworks in Python provide. So go learn a framework. If Django seems too complicated, try Flask first.
Also don't try and do it on AWS from the outset, learn the frameworks by using the development servers they provide on your own box. Just work through their respective tutorials.
Actually AWS Elastic Beanstalk give you pure Python (2.6/2.7/3.4) with Apache + mod_wsgi as web proxy. You can look all supported Python environment in here. So, you can use any Python web framework (such as Django, Flask, etc.) in your web app. If you can, choose common and supported framework by AWS (Django/Flask).
You can think: Python + Apache + mod_wsgi is equivalent to PHP + Apache + mod_php.
Please take a look into AWS Elastic Beanstalk documentation for how to working with Python here. Read the Deploying a Django Application or Deploying a Flask Application if you choose one of them. You need to provide what Elastic Beanstalk environment (mod_wsgi) needs.
Same as PHP, Python actually only copy and paste the files. If you want to make Python web app without framework, you need to follow the WSGI standard. You can take look into this question. In my opinion, better if you use a framework, because it handles the WSGI part for you.
First of all this is a good blog post to start from if you are using Django
I don't know much about Flask, with Django once you understand the core concepts it's not hard at all.
application.py is the file that aws looks for as stated in the blog post I pointed to:
By default eb assumes our wsgi file is called application.py
this can be changed to your local wcgi.py file that Django makes when you start your project with django.
Beware that you want to use your static url correctly so aws will read them from the right folder. I personally disagree about the way the static files configuration in the post.
It's better to stay with the aws default which is "static" and just set static url in django settings to "/static/"

Django deployment final step

I have a Django project with 1 app that is working locally and I am trying to make it work on the server but I imagine I am still missing something...
The steps I have followed are:
1) create a virtualnev
2) Install django and the libraries I need
3) copy my local project to the server, keeping the same directory structure
4) create the file passenger_wsgi.py (python passenger_wsgi.py did not return any error)
After this do I need to do anything like python manage.py runserver? Or with this I should be already able to see the site through mydomain/my project/ my app (when I do ot just get an error 404)?
I have read the django book and followed the tutorial, but this part is not well described anywhere...
Thanks in advance for any help!
Deployment is explained in the documentation.
You need to actually serve your application with some kind of HTTP server and something to run your python code. Some of the possible combinations are:
nginx with uWSGI
Any web server as reverse proxy with gunicorn
Apache with mod_wsgi
Your hosting service may or may not give you the choice or even the possibilty to do this.
There is a list of Django friendly hosters in the Django wiki.
Well firstly you might want to go through the previously asked questions.
When you are deploying using passenger you do not need to run manage.py runserver , etc. The passenger_wsgi file should take care of it.
You might want to check this link out, in the first answer also contains link to Dreamhost which details quite extensively on how to achieve the same.
Visit Deploying Django app using passenger
From my personal experience I found Nginx and uwsgi setup to be much more easier to handle and work and debug in the logs, but this is subjective to your needs and platform you may have.

Minimal working heroku django app

I have been having some problems with deploying my django app to heroku. I have triple checked the getting started guide several times and I am getting quite frustrated. The most annoying thing is that I don't get any errors during deployment, in heroku logs or during local deployment. Here is an SO question with more details.
The easiest solution at this stage would probably be to reverse engineer a minimal working django app which I could push straight to my heroku repo without any extra config.
(The smaller the app the better a simple <h1>hello world</h1> would do but it would need to include venv, requirements.txt and of course Procfile)
Thanks
Take your pick. Either of these should work for you.
https://github.com/auzigog/django-template-heroku
https://github.com/cyberdelia/django-heroku-template
You can try http://django.ironcoder.me - visual composer, which deploys working Django app to your heroku account.

A list of everything I need for running my app on a web server

What I want to know are actually 2 things.
Part 1:
I have a developed source code for an application in python that uses Django framework.
What I want is to be able to run the code on a developer machine and see the result. What do I need for it?
(my guesses):
Python development enironment (Eclipse/PyDev/Aptana Studio 3 seem to be the better ones for windows not sure linux yet),
I also have a postgre database already setup (I know there's a file where I have to specify connection information)
- something installed from django or would this be already included in the code that I have?
Part II:
I also want to make a dev server accessible through internet.
- this is the major part of the question and the most important. How do I publish the app?
- I have a linux machine that I would do this on, but unsure of all the things I need. Apache server?
To answer your questions:
What you need: A list of requirements and instructions to get started with Django is available here: http://djangobook.com/en/2.0/chapter02/.
Database: that chapter also includes a section on configuring access to your database, with a specific section on postgreSQL.
Dev server: To start a basic development server, see this tutorial section
Deploying django (production): For instructions on how to deploy Django for production, see chapter on deploying Django.
Publishing on internet: as for making your dev server accessible through the internet, ask on https://serverfault.com/. Make sure you provide more information about your network setup, what you've tried, what isn't working, etc. (Briefly, you need to make sure that the host you are running your server on is on a publicly accessible IP, or has port 80 forwarded to it from such a host. If in doubt, speak to your sys/network admin if you have one. Or use a django hosting service such as those listed on http://djangohosting.com)
IDE : Regarding IDE, it's down to personal preference. What you mentioned are fine and can run on Linux too.
As a first step, I suggest you follow the tutorial which guides you through the process of starting a development server and developing a basic app.
Even if your goal is to deploy an existing app, the tutorial will give you an idea of how the different components work together (apps, models, urls, templates, etc) which will help with debugging when something goes wrong with your deployment.
Good luck.
You need Python, Django, a WSGI container (e.g. mod_wsgi, uWSGI, Paste Deploy), and a database server. You make the Django project available as a WSGI app, bound to the appropriate interface on the machine.

Categories

Resources