Skip to content

Commit b54c941

Browse files
committed
Simplified quick intro for experienced users. By: Harry
1 parent 537badb commit b54c941

1 file changed

Lines changed: 84 additions & 92 deletions

File tree

content/django.txt

Lines changed: 84 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -20,66 +20,110 @@ account. Go and sign up if you don't already have one then come back here.
2020

2121

2222

23-
The really quick start guide
23+
The really quick start guide, for experienced Django users
2424
=================================================================================
2525

26-
To start with, for those people who are already very confident with Django we will
27-
just start with a list of the steps and commands you need to run in order to get
28-
a working Django app on PythonAnywhere. Don't worry if you don't understand these
29-
now. We will go through each one, step by step, further down.
26+
The idea is that it should be just as easy to host your project on PythonAnywhere, as
27+
it is to host in on your own PC using the Django dev server.
3028

31-
So, the really quick start guide:
29+
There's just a couple of subtleties:
3230

33-
Create the project files
31+
1. adding the right path to ``sys.path`` in ``wsgi.py``
32+
2. setting up your database in ``settings.py`` - you'll need the full path for sqlite
33+
3. using ``staticfiles_urlpatterns`` in ``urls.py`` to serve static media.
34+
35+
If you're only just starting out with Django, you probably just want to skim through this
36+
stuff, don't worry if you don't understand it, and move onto the more detailed guide.
37+
All will become clear!
38+
39+
40+
Note down the path to your project's *parent* folder and the project name
3441
---------------------------------------------------------------------------------
3542

36-
Open a bash console on Python Anywhere and enter the following commands
43+
There are several ways you might have got a Django project into PythonAnywhere -
44+
maybe you started one from scratch using ``django-admin.py startproject``. Maybe
45+
you pulled it in from GitHub or another code sharing site using ``git`` or a similar
46+
VCS tool. Maybe it's in your Dropbox!
3747

38-
::
48+
Either way, the thing to do is make a note of the path to **parent folder of
49+
the project root**. The project root is the folder which contains ``settings.py``;
50+
for example, let's say it's ``/home/my_username/projects/my_project/``
3951

40-
django-admin.py startproject <my project name>
41-
cd <my project name>
42-
python manage.py startapp <my app name>
52+
In this case, you want to make a note of the path to the project's parent folder
53+
54+
* ``/home/my_username/projects``
55+
56+
You also need to make a note of the **name of the project root folder** in this case:
57+
58+
* ``my_project``
59+
60+
Those two together should add up to the full path to the project root. Crystal-clear?
4361

4462

4563
Edit the wsgi file
4664
---------------------------------------------------------------------------------
4765

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.
66+
Go to your PythonAnywhere *Dashboard* and click on the *Web* tab, then click on
67+
the link to **edit your WSGI file**.
68+
69+
Delete the contents and replace them with the below, replacing
70+
``/home/my_username/projects`` with the path to the parent folder of your project,
71+
which you noted down earlier, and ``my_project`` with your project name.
5272

5373
.. sourcecode:: python
5474

5575
# +++++++++++ DJANGO +++++++++++
5676
import os
5777
import sys
5878

59-
## assuming your Django settings file is at '/home/<my name>/<my project name>/settings.py'
60-
path = '/home/<my name>/'
79+
## assuming your Django settings file is at '/home/my_username/projects/my_project/settings.py'
80+
path = '/home/my_username/projects'
6181
if path not in sys.path:
6282
sys.path.append(path)
6383

64-
os.environ['DJANGO_SETTINGS_MODULE'] = '<my project name>.settings'
84+
os.environ['DJANGO_SETTINGS_MODULE'] = 'my_project.settings'
6585

6686
import django.core.handlers.wsgi
6787
application = django.core.handlers.wsgi.WSGIHandler()
6888

69-
You should now have a default Django app which you can visit at <my name>.pythonanywhere.com
89+
You should now have a default Django app which you can visit at
90+
http://my_username.pythonanywhere.com
91+
92+
If you have any problems, it may be to do with the ``sys.path``, and how it
93+
interacts with ``import`` statements. We've assumed that all your project
94+
imports look like:
95+
96+
.. sourcecode:: python
97+
98+
from my_project.myapp.models import Kitchen, Sink
99+
100+
If instead you've used:
101+
102+
.. sourcecode:: python
103+
104+
from myapp.models import Kitchen, Sink
105+
106+
You may need to fully specify the ``my_project.``
70107

71-
Edit settings.py
108+
109+
Setup the database in settings.py, and syncdb
72110
---------------------------------------------------------------------------------
73111

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.
112+
You need to make sure of three things:
113+
114+
* if using *sqlite*, you must have the full path to your database
115+
116+
* if using *MySql*, you'll need the database name, password, and host
117+
(``mysql.server`` if you're using our MySQL service)
118+
119+
* finally, make sure all your apps are in INSTALLED_APPS
76120

77121
.. sourcecode:: python
78122

79123
DATABASES = {
80124
'default': {
81125
'ENGINE': 'django.db.backends.sqlite3',
82-
'NAME': '/home/<my name>/my_test_project/db.sqlite', # absolute location is required
126+
'NAME': '/home/my_username/my_test_project/db.sqlite', # absolute location is required
83127
'USER': '',
84128
'PASSWORD': '',
85129
'HOST': '',
@@ -94,99 +138,47 @@ database and installed apps sections to match the code below.
94138
)
95139

96140

97-
Create empty database and edit urls.py
98-
---------------------------------------------------------------------------------
99-
100-
Now it's back to your bash console to perform the initial database creation
141+
Now open up a **Bash console** to perform the initial database creation
101142

102143
::
103144

104145
cd <your project name>
105146
./manage.py syncdb
106147

107-
This will ask you to create an admin user. Follow the prompts and you remember
108-
the password. You will need it to login to the admin interface.
148+
Follow the usual prompts to create an admin user and password.
149+
150+
151+
Editing urls.py to serve static files
152+
---------------------------------------------------------------------------------
153+
154+
Next you will need to edit the ``urls.py``. Uncomment the admin lines if you need
155+
to, but most importantly *use* ``staticfiles_urlpatterns`` *to serve static pages*.
109156

110-
Next you will need to edit the urls.py file which can be found inside your
111-
project folder. Make it look like the one below but insure you substitute the
112-
project and app names that you used previously.
113157

114158
.. sourcecode:: python
115159

116160
from django.conf.urls.defaults import patterns, include, url
117-
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
118-
119-
# Uncomment the next two lines to enable the admin:
161+
120162
from django.contrib import admin
121163
admin.autodiscover()
122164

123165
urlpatterns = patterns('',
124-
# Examples:
125-
url(r'^$', '<my project name>.<my app name>.views.home', name='home'),
126-
# url(r'^<my project name>/', include('<my project name>.foo.urls')),
127-
128-
# Uncomment the admin/doc line below to enable admin documentation:
129-
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
130-
131-
# Uncomment the next line to enable the admin:
166+
# [EG] url(r'^<my project name>/', include('<my project name>.foo.urls')),
132167
url(r'^admin/', include(admin.site.urls)),
133168
)
134169
# This is needed to serve static files like images and css
170+
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
135171
urlpatterns += staticfiles_urlpatterns()
136172

137-
At this stage you have a working admin interface and can visit it at
138-
<my name>.pythonanywhere.com/admin. We are almost finished. Next we will go back
139-
to our bash console and create a templates folder and blank template.
140-
141-
142-
Create a root template
143-
---------------------------------------------------------------------------------
144-
145-
::
146-
147-
mkdir <my project name>/templates
148-
touch <my project name>/templates/home.html
149-
150-
Now we will edit the home.html template file so that it looks like this.
151-
152-
.. sourcecode:: html+django
153-
154-
<html>
155-
<head>
156-
<title>My Python Anywhere hosted Django app</title>
157-
</head>
158-
<body>
159-
<h1>My Python Anywhere hosted Django app</h1>
160-
<p>Well, since it's already {{ right_now.minute }} past {{ right_now.hour }} UTC,
161-
that is as far as we are going to take you in this tutorial.</p>
162-
<p>What you do next is up to you...</p>
163-
</body>
164-
</html>
165-
166-
167-
Create your first view in views.py
168-
---------------------------------------------------------------------------------
169-
170-
The final piece of work we need to do is create a view inside the file found at
171-
<my project name>/<my app name>/views.py. Make it look like this.
172-
173-
.. sourcecode:: python
174-
175-
from datetime import datetime
176-
177-
from django.shortcuts import render
178-
179-
def home(request):
180-
return render(request, 'home.html', {'right_now':datetime.utcnow()})
181-
182173

183174
Reload the web server and enjoy!
184175
---------------------------------------------------------------------------------
185176

186177
Now you just need to reload the web server so that it notices the changes you
187178
have made. Visit the Web tab on the Python Anywhere dashboard and click the
188-
"Reload web app" button. That's it. You can now see your newly created web app
189-
serving a page that tells you the time.
179+
"Reload web app" button. That's it. At this stage you have a working admin
180+
interface and can visit it at
181+
http://my_username.pythonanywhere.com/admin.
190182

191183

192184

@@ -258,7 +250,7 @@ Your next step is to edit the wsgi.py file which is located at /var/www/wsgi.py.
258250
Editing your wsgi.py file
259251
---------------------------------------------------------------------------------
260252

261-
The wsgi.py file is a Python Anywhere specific python script and cannot be moved
253+
The wsgi.py file is a Python Anywhere specific Python script and cannot be moved
262254
or renamed. You can find a direct link to it on the Web tab of the dashboard.
263255
Go there now and edit the file so that it matches the one below. Making sure to
264256
replace the <my name> amd <my project name> with the your username and the

0 commit comments

Comments
 (0)