1+ =================================================================================
12Creating a new django project on Python Anywhere
23=================================================================================
3- The five minute guide
4- ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
5-
64
75So you want to create a web application but you don't really want to do all the
86faffing around that is involved in setting up and configuring web servers?
@@ -14,7 +12,7 @@ interface and a front page that tells you the time.
1412To follow along with this tutorial you will need a `Python Anywhere <http://www.pythonanywhere.com/>`_
1513account. Go and sign up if you don't already have one then come back here.
1614
17- .. contents::
15+ .. contents:: Table of Contents
1816
1917
2018
@@ -23,19 +21,176 @@ account. Go and sign up if you don't already have one then come back here.
2321
2422
2523The really quick start guide
26- ---------------------------------------------------------------------------------
24+ =================================================================================
2725
2826To start with, for those people who are already very confident with django we will
2927just start with a list of the steps and commands you need to run in order to get
3028a working django app on Python Anywhere. Don't worry if you don't understand these
31- now. We will go through each one step by step further down.
29+ now. We will go through each one, step by step, further down.
30+
31+ So, the really quick start guide:
32+
33+ Create the project files
34+ ---------------------------------------------------------------------------------
35+
36+ Open a bash console on Python Anywhere and enter the following commands
37+
38+ ::
39+
40+ django-admin.py startproject <my project name>
41+ cd <your project name>
42+ python manage.py startapp <my app name>
43+
44+
45+ Edit the wsgi file
46+ ---------------------------------------------------------------------------------
47+
48+ Go back to your dashboard and click on the web tab, then click on the link to
49+ edit your wsgi.py file. Delete the contents and copy the code below into it.
50+ Changing "<my name>" to be your Python Anywhere username and <my project name>
51+ so that it matches the value you entered above.
52+
53+ ::
54+
55+ # +++++++++++ DJANGO +++++++++++
56+ import os
57+ import sys
58+
59+ ## assuming your django settings file is at '/home/<my name>/<my project name>/settings.py'
60+ path = '/home/<my name>/'
61+ if path not in sys.path:
62+ sys.path.append(path)
63+
64+ os.environ['DJANGO_SETTINGS_MODULE'] = '<my project name>.settings'
65+
66+ import django.core.handlers.wsgi
67+ application = django.core.handlers.wsgi.WSGIHandler()
3268
69+ You should now have a default django app which you can visit at <my name>.pythonanywhere.com
70+
71+ Edit settings.py
72+ ---------------------------------------------------------------------------------
73+
74+ Now you need to edit settings.py inside the project directory and change the
75+ database and installed apps sections to match the code below.
76+
77+ ::
78+
79+ DATABASES = {
80+ 'default': {
81+ 'ENGINE': 'django.db.backends.sqlite3',
82+ 'NAME': '/home/%s/my_test_project/db.sqlite',
83+ 'USER': '',
84+ 'PASSWORD': '',
85+ 'HOST': '',
86+ 'PORT': ''
87+ }
88+ }
89+
90+ INSTALLED_APPS += (
91+ 'django.contrib.admin',
92+ 'my_test_project.my_app',
93+ )
94+
95+
96+ Create empty database and edit urls.py
97+ ---------------------------------------------------------------------------------
98+
99+ Now it is back to your bash console to perform the initial database creation
100+
101+ ::
33102
34- header 2
35- +++++++++++++++++++++++++
103+ cd <your project name>
104+ ./manage.py syncdb
36105
37- header 3
38- ________________________
106+ This will ask you to create an admin user. Follow the prompts and you remember
107+ the password. You will need it to login to the admin interface.
108+
109+ Next you will need to edit the urls.py file which can be found inside your
110+ project folder. Make it look like the one below but insure you substitute the
111+ project and app names that you used previously.
112+
113+ ::
114+
115+ from django.conf.urls.defaults import patterns, include, url
116+ from django.contrib.staticfiles.urls import staticfiles_urlpatterns
117+
118+ # Uncomment the next two lines to enable the admin:
119+ from django.contrib import admin
120+ admin.autodiscover()
121+
122+ urlpatterns = patterns('',
123+ # Examples:
124+ url(r'^$', '<my project name>.<my app name>.views.home', name='home'),
125+ # url(r'^<my project name>/', include('<my project name>.foo.urls')),
126+
127+ # Uncomment the admin/doc line below to enable admin documentation:
128+ # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
129+
130+ # Uncomment the next line to enable the admin:
131+ url(r'^admin/', include(admin.site.urls)),
132+ )
133+ # This is needed to serve static files like images and css
134+ urlpatterns += staticfiles_urlpatterns()
135+
136+ At this stage you have a working admin interface and can visit it at
137+ <my name>.pythonanywhere.com/admin. We are almost finished. Next we will go back
138+ to our bash console and create a templates folder and blank template.
139+
140+
141+ Create a root template
142+ ---------------------------------------------------------------------------------
143+
144+ ::
145+
146+ mkdir <my project name>/templates
147+ touch <my project name>/templates/home.html
148+
149+ Now we will edit the home.html template file so that it looks like this.
150+
151+ ::
152+
153+ <html>
154+ <head>
155+ <title>My Python Anywhere hosted Django app</title>
156+ </head>
157+ <body>
158+ <h1>My Python Anywhere hosted Django app</h1>
159+ <p>Well, since it's already {{ right_now.minute }} past {{ right_now.hour }} UTC,
160+ that is as far as we are going to take you in this tutorial.</p>
161+ <p>What you do next is up to you...</p>
162+ </body>
163+ </html>
164+
165+
166+ Create your first view in views.py
167+ ---------------------------------------------------------------------------------
168+
169+ The final piece of work we need to do is create a view inside the file found at
170+ <my project name>/<my app name>/views.py. Make it look like this.
171+
172+ ::
173+
174+ from datetime import datetime
175+
176+ from django.shortcuts import render
177+
178+ def home(request):
179+ return render(request, 'home.html', {'right_now':datetime.utcnow()})
180+
181+
182+ Reload the web server and enjoy!
183+ ---------------------------------------------------------------------------------
184+
185+ Now you just need to reload the web server so that it notices the changes you
186+ have made. Visit the Web tab on the Python Anywhere dashboard and click the
187+ "Reload web app" button. That's it. You can now see your newly created web app
188+ serving a page that tells you the time.
189+
190+
191+
192+ The gruesome details for beginners
193+ =================================================================================
39194
40195
41196Starting a new django project
@@ -75,7 +230,7 @@ Writing the first view
75230
76231
77232Python Anywhere specific tips
78- ---------------------------------------------------------------------------------
233+ =================================================================================
79234
80235
81236
0 commit comments