Skip to content

Commit 5241f27

Browse files
author
Resolver Developers
committed
Completes text of tutorial and adds syntax highlighting for code
examples. By Hansel.
1 parent b5ff647 commit 5241f27

4 files changed

Lines changed: 311 additions & 28 deletions

File tree

content/django.txt

Lines changed: 215 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Open a bash console on Python Anywhere and enter the following commands
3838
::
3939

4040
django-admin.py startproject <my project name>
41-
cd <your project name>
41+
cd <my project name>
4242
python manage.py startapp <my app name>
4343

4444

@@ -50,7 +50,7 @@ edit your wsgi.py file. Delete the contents and copy the code below into it.
5050
Changing "<my name>" to be your Python Anywhere username and <my project name>
5151
so that it matches the value you entered above.
5252

53-
::
53+
.. sourcecode:: python
5454

5555
# +++++++++++ DJANGO +++++++++++
5656
import os
@@ -74,7 +74,7 @@ Edit settings.py
7474
Now you need to edit settings.py inside the project directory and change the
7575
database and installed apps sections to match the code below.
7676

77-
::
77+
.. sourcecode:: python
7878

7979
DATABASES = {
8080
'default': {
@@ -110,7 +110,7 @@ Next you will need to edit the urls.py file which can be found inside your
110110
project folder. Make it look like the one below but insure you substitute the
111111
project and app names that you used previously.
112112

113-
::
113+
.. sourcecode:: python
114114

115115
from django.conf.urls.defaults import patterns, include, url
116116
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
@@ -148,7 +148,7 @@ Create a root template
148148

149149
Now we will edit the home.html template file so that it looks like this.
150150

151-
::
151+
.. sourcecode:: html+django
152152

153153
<html>
154154
<head>
@@ -169,7 +169,7 @@ Create your first view in views.py
169169
The final piece of work we need to do is create a view inside the file found at
170170
<my project name>/<my app name>/views.py. Make it look like this.
171171

172-
::
172+
.. sourcecode:: python
173173

174174
from datetime import datetime
175175

@@ -196,68 +196,258 @@ The main web tab at Python Anywhere looks like this:
196196

197197
.. image:: http://www.pythonanywhere.com/static/anywhere/images/tutorial/django/pa_web_dashboard.jpg
198198

199-
To edit your wsgi.py file you click the link with that name. Every time you
200-
make a change to the files on the Python Anywhere servers you need to click the
201-
"Reload web app" button. This reloads the web server so that it notices the
202-
changes you have made.
199+
To edit your wsgi.py file you click the link titled "this WSGI file". Every
200+
time you make a change to the files on the Python Anywhere servers you need to
201+
click the "Reload web app" button at the bottom of the page. This reloads the
202+
web server so that it notices the changes you have made.
203+
204+
You can also follow the links through to your error and access logs. These are
205+
very handy for figuring out why the code you have written might not be working
206+
or seeing who has been visiting your respectively. If your web app is giving
207+
a message like "500 internal server error" then you should visit your error log
208+
and see the actual error message that your code is generating.
203209

210+
The WSGI file here is how you tell Python Anywhere what application you actually
211+
want to run. Any WSGI web framework can be used but in this tutorial we will only
212+
be focusing on django.
204213

205214

206215
Starting a new django project
207216
---------------------------------------------------------------------------------
208217

218+
Django is already installed on Python Anywhere so you it is easy to get started.
219+
220+
221+
.. image:: http://www.pythonanywhere.com/static/anywhere/images/tutorial/django/pa_console_dashboard.jpg
222+
223+
The first commands need to be run inside a bash console. You will need to go to
224+
the "Consoles" tab of Python Anywhere and click on the link highlighted in red
225+
here to create a new bash console.
226+
227+
You will then need to type the following at
228+
the console prompt.
229+
230+
Throughout this tutorial there will be terms surrounded by
231+
angle brackets <like this>. This means that you should replace the contents,
232+
including the angle brackets with your own name for the project and application
233+
that you are creating. So for example the first command below might actually
234+
look like this:
235+
236+
"django-admin.py startproject blog".
237+
238+
Remember this convention because if you just copy and paste the example commands
239+
they won't work and you will receive errors rather than the expected results.
240+
241+
So, enter the code below into a bash console:
242+
243+
::
244+
245+
django-admin.py startproject <my project name>
246+
cd <my project name>
247+
python manage.py startapp <my app name>
248+
249+
Below is a an example of what your console should look like after you have done
250+
this.
251+
252+
.. image:: http://www.pythonanywhere.com/static/anywhere/images/tutorial/django/pa_console_django_start.jpg
253+
254+
Your next step is to edit the wsgi.py file which is located at /var/www/wsgi.py.
209255

210256

211257
Editing your wsgi.py file
212258
---------------------------------------------------------------------------------
213259

260+
The wsgi.py file is a Python Anywhere specific python script and cannot be moved
261+
or renamed. You can find a direct link to it on the Web tab of the dashboard.
262+
Go there now and edit the file so that it matches the one below. Making sure to
263+
replace the <my name> amd <my project name> with the your username and the
264+
project name that you chose in the previous step.
265+
266+
267+
.. sourcecode:: python
268+
269+
# +++++++++++ DJANGO +++++++++++
270+
import os
271+
import sys
272+
273+
## assuming your django settings file is at '/home/<my name>/<my project name>/settings.py'
274+
path = '/home/<my name>/'
275+
if path not in sys.path:
276+
sys.path.append(path)
277+
278+
os.environ['DJANGO_SETTINGS_MODULE'] = '<my project name>.settings'
279+
280+
import django.core.handlers.wsgi
281+
application = django.core.handlers.wsgi.WSGIHandler()
282+
283+
284+
At this point you have a very basic default django app running. You should visit
285+
it to make sure that this is the case. Every Python Anywhere user has a default
286+
place to visit their web app. It is <my username>.pythonanywhere.com. This
287+
tutorial is in fact being delivered to you by a django app run by the user
288+
"tutorial". If your username was harry you would access your app at
289+
http://harry.pythonanywhere.com. You can find a link to your own address at the
290+
Web tab in the dashboard. Try going their now and checking that you can see the
291+
default welcome message from django then come back here to continue.
214292

215293

216294
Configuring the database and enabling the admin interface
217295
---------------------------------------------------------------------------------
218296

297+
Django needs a database connection to do pretty much anything. Python Anywhere
298+
provides support for MySQL databases but for now we will just use sqlite3. A file
299+
based database that does not require setting up a database account and password.
300+
301+
The file that contains all the settings information for django is called,
302+
naturally enough, settings.py. By default it lives in your django project
303+
directory. You will need to open this file up and change the DATABASE and
304+
INSTALLED_APPS sections of it so that it looks like the code below. Do not change
305+
any other bits of it at this time.
306+
307+
.. sourcecode:: python
308+
309+
DATABASES = {
310+
'default': {
311+
'ENGINE': 'django.db.backends.sqlite3',
312+
'NAME': '/home/%s/<my_test_project>/db.sqlite',
313+
'USER': '',
314+
'PASSWORD': '',
315+
'HOST': '',
316+
'PORT': ''
317+
}
318+
}
319+
320+
INSTALLED_APPS += (
321+
'django.contrib.admin',
322+
'<my project name>.<my app name>',
323+
)
324+
325+
Now that you have told django what database to use you have to run a management
326+
command in order for it to create the initial tables and the first admin user.
327+
In order to do this you go back to a bash shell and enter the following commands
328+
329+
::
330+
331+
cd <your project name>
332+
./manage.py syncdb
333+
334+
You will be asked a series of questions. You should enter a username, email
335+
address, and password for the first admin user. You will need this information
336+
to log in to django's admin interface so make sure that you remember the details
337+
somehow.
219338

220339

221340
Defining your urls
222341
---------------------------------------------------------------------------------
223342

343+
The next step is defining the urls for your application. This means you are
344+
starting to tell django what to do when a user visits a certain location in your
345+
web site. The file to edit is called urls.py and it should be in the same
346+
directory as your settings.py file. Open it up and make it look like the file
347+
below. Remember, as always, that each time you see a phrase inside angle
348+
brackets you need to replace it with the project and app name that you have
349+
chosen.
224350

351+
.. sourcecode:: python
225352

353+
from django.conf.urls.defaults import patterns, include, url
354+
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
355+
356+
# Uncomment the next two lines to enable the admin:
357+
from django.contrib import admin
358+
admin.autodiscover()
359+
360+
urlpatterns = patterns('',
361+
# Examples:
362+
url(r'^$', '<my project name>.<my app name>.views.home', name='home'),
363+
# url(r'^<my project name>/', include('<my project name>.foo.urls')),
364+
365+
# Uncomment the admin/doc line below to enable admin documentation:
366+
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
367+
368+
# Uncomment the next line to enable the admin:
369+
url(r'^admin/', include(admin.site.urls)),
370+
)
371+
# This is needed to serve static files like images and css
372+
urlpatterns += staticfiles_urlpatterns()
373+
374+
In this file you are defining two url patterns. The first one matches a blank
375+
string which is what happens when a user visits the <your username>.pythonanywhere.com.
376+
The second one matches "admin/" which is djangos default admin interface. This
377+
should now be working. If you like you can take a look at it now, logging in with
378+
the username and password that you provided earlier. When you are done come back
379+
here to continue. You may need to press the "Reload web app" button on your on
380+
the web tab before theses changes will be recognised. Do that if it doesn't work
381+
the first time you visit it.
226382

383+
If you get an error rather than the admin interface then go back through each of
384+
these steps and check that everything is exactly like the examples. You also
385+
might want to check the error logs, available from the web tab, and see if they
386+
can give you additional clues about where the the mistake might be.
227387

228388
Creating a template
229389
---------------------------------------------------------------------------------
230390

391+
Now that you have a working admin interface it is time to create a template.
231392

393+
Go back to your bash console and create a directory for your template and an
394+
empty template file using the commands below.
232395

396+
::
233397

398+
mkdir <my project name>/templates
399+
touch <my project name>/templates/home.html
234400

235-
Writing the first view
236-
---------------------------------------------------------------------------------
401+
Now go back to your files and edit the home.html template file so that it looks
402+
like this.
237403

404+
.. sourcecode:: html+django
238405

406+
<html>
407+
<head>
408+
<title>My Python Anywhere hosted Django app</title>
409+
</head>
410+
<body>
411+
<h1>My Python Anywhere hosted Django app</h1>
412+
<p>Well, since it's already {{ right_now.minute }} past {{ right_now.hour }} UTC,
413+
that is as far as we are going to take you in this tutorial.</p>
414+
<p>What you do next is up to you...</p>
415+
</body>
416+
</html>
239417

418+
The values inside the "{{ }}" are going to be replaced by dynamic content when
419+
we complete our final task. Which is writing a view.
240420

241421

242-
Python Anywhere specific tips
243-
=================================================================================
422+
Writing the first view
423+
---------------------------------------------------------------------------------
244424

425+
Views are django functions which take a request and return a response. We are
426+
going to write a very simple view called home which uses the "home.html" template
427+
and uses the datetime module to tell us what the time is whenever the page is
428+
refreshed. The file we need to edit is called views.py and it will be inside
429+
the <my app name> folder inside the directory containing settings.py.
245430

431+
Copy the code below into it and save the file.
246432

247433

434+
.. sourcecode:: python
435+
436+
from datetime import datetime
437+
438+
from django.shortcuts import render
439+
440+
def home(request):
441+
return render(request, 'home.html', {'right_now':datetime.utcnow()})
248442

443+
The last step is reloading your web app so that the changes are noticed by the
444+
web server. Go and do that now.
249445

446+
If you have followed along with this tutorial you should now have a working,
447+
dynamic page at <your username>.pythonanywhere.com. You can continue to
448+
experiment with this by changing the view and the template as well as read
449+
through the full django documentation https://docs.djangoproject.com to see what
450+
else is possible.
250451

251-
::
252452

253-
import os
254-
import sys
255-
## assuming your django settings file is at '/home/myname/mysite/settings.py'
256-
path = '/home/%s/'
257-
if path not in sys.path:
258-
sys.path.append(path)
259-
os.environ['DJANGO_SETTINGS_MODULE'] = 'my_test_project.settings'
260-
import django.core.handlers.wsgi
261-
application = django.core.handlers.wsgi.WSGIHandler()
262453

263-
Once your wsgi.py file looks like this we can move on.

0 commit comments

Comments
 (0)