Create a Python 'Hello World' App with Django
Posted by ASO Admin on 11 February 2020 08:15 AM
|
|
Note: this tutorial is focused on Shared hosting environments only. Preparing the Python EnvironmentThe first thing we need to do is set up a local Python environment. For this we'll be using VirtualEnv, which is commonly used to isolate Python environments. This tutorial will require SSH access to your account, which is covered <here>. Also, in several places we refer to your account's home directory, and use "%USER%" (without the quotes) to refer to your account username. Make sure to replace every occurrence of "%USER%" with your actual account username. First, log in to your account via SSH. If you're already logged in, let's ensure we're in the base of your home directory by entering: cd ~ Next we'll initialize our local python environment under the "pyenv" directory, relative to your home directory (/home/%USER%/pyenv): virtualenv --python=python2.6 pyenvsource pyenv/bin/activate After that last command, our prompt should now look a little different indicating we've entered our python environment. Now let's install Django: pip install django Creating Your First Django ApplicationJust in case you're revisiting this tutorial, please ensure we're still in or change to your account's home directory: cd ~ Now that our environment has been set up to support Django, we can create our first Django based project. For this tutorial we're going to create a standard Hello World application. First, let's create our django project: ~/pyenv/bin/django-admin.py startproject whyhellothere And now change to our application's directory: cd whyhellothere Now we need to finish creating our application directory layout: mkdir publicmkdir tmp Now, let's create a view for our application: vi whyhellothere/views.py Inside this file, place these contents to create our first view: from django.http import HttpResponse def hello(request): return HttpResponse("Hello World") Next, we need to connect our view to a URL: vi whyhellothere/urls.py At the top of this file, directly below the line "from django.conf.urls import patterns, include, url", add the following: from whyhellothere.views import hello Then, later in the file, you'll see " url(r'^admin/', include(admin.site.urls)),". Directly above this line, add: url(r'^$', hello), Save and then close the file. Next we need to set up our static assets to serve them properly, open the following file: vi whyhellothere/settings.py At the bottom of the file, you should find a line that looks like "STATIC_URL = '/static/'", we need to change this to: STATIC_URL = '/' And directly below that line, add: STATIC_ROOT = '/home/%USER%/whyhellothere/public/' Save the file and close it. Now you'll need to collect the static assets from your new Django app. Run the following command: ~/pyenv/bin/python manage.py collectstatic It should prompt a yes or no question about replacing files in the directory we gave above for the STATIC_ROOT setting. Enter 'yes'. Set Up Your New Django App for PassengerWe're almost done! Next, we need to create a file named "passenger_wsgi.py". This is the entry file to our application: vi "passenger_wsgi.py" Inside this file, place the below contents substituting %USER% for your own user on the server: import sys, os virt_binary = "/home/%USER%/pyenv/bin/python" if sys.executable != virt_binary: os.execl(virt_binary, virt_binary, *sys.argv) sys.path.append(os.getcwd()) os.environ['DJANGO_SETTINGS_MODULE'] = "whyhellothere.settings" import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler() Save and close the file. The final step is to connect our application to our domain's document root. First, remove the old directory: rm -fr /home/%USER%/public_html Replace it with a symlink to our application's public folder: ln -s /home/%USER%/whyhellothere/public /home/%USER%/public_html Save and close the file. Now try visiting your website, you should see "Hello World". | |
|