Skip to content

Commit 3af4526

Browse files
committed
Remove need for whitenoise and collectstatic
Enabling the same WSGI middleware that is used by runserver allows us to simplify the static files process, and remove a bunch of steps
1 parent 203401c commit 3af4526

File tree

5 files changed

+18
-52
lines changed

5 files changed

+18
-52
lines changed

en/css/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Finally we will take a closer look at these things we've been calling __static f
3838

3939
### Where to put static files for Django
4040

41-
As you saw when we ran `collectstatic` on the server, Django already knows where to find the static files for the built-in "admin" app. Now we just need to add some static files for our own app, `blog`.
41+
Django already knows where to find the static files for the built-in "admin" app. Now we just need to add some static files for our own app, `blog`.
4242

4343
We do that by creating a folder called `static` inside the blog app:
4444

@@ -66,7 +66,7 @@ Time to write some CSS! Open up the `blog/static/css/blog.css` file in your code
6666

6767
We won't be going too deep into customizing and learning about CSS here. It's pretty easy and you can learn it on your own after this workshop. There is a recommendation for a free course to learn more at the end of this page.
6868

69-
But let's do at least a little. Maybe we could change the color of our header?
69+
But let's do at least a little. Maybe we could change the color of our header?
7070
To understand colors, computers use special codes. These codes start with `#` followed by 6 letters (A-F) and numbers (0-9). For example, the code for blue is `#0000FF`. You can find the color codes for many colors here: http://www.colorpicker.com/. You may also use [predefined colors](http://www.w3schools.com/cssref/css_colornames.asp), such as `red` and `green`.
7171

7272
In your `blog/static/css/blog.css` file you should add the following code:
@@ -79,7 +79,7 @@ h1 a {
7979

8080
`h1 a` is a CSS Selector. This means we're applying our styles to any `a` element inside of an `h1` element. So when we have something like: `<h1><a href="">link</a></h1>` the `h1 a` style will apply. In this case, we're telling it to change its color to `#FCA205`, which is orange. Of course, you can put your own color here!
8181

82-
In a CSS file we determine styles for elements in the HTML file. The first way we identify elements is with the element name. You might remember these as tags from the HTML section. Things like `a`, `h1`, and `body` are all examples of element names.
82+
In a CSS file we determine styles for elements in the HTML file. The first way we identify elements is with the element name. You might remember these as tags from the HTML section. Things like `a`, `h1`, and `body` are all examples of element names.
8383
We also identify elements by the attribute `class` or the attribute `id`. Class and id are names you give the element by yourself. Classes define groups of elements, and ids point to specific elements. For example, you could identify the following tag by using the tag name `a`, the class `external_link`, or the id `link_to_wiki_page`:
8484

8585
```html
@@ -94,13 +94,13 @@ Then, we need to also tell our HTML template that we added some CSS. Open the `b
9494
{% load staticfiles %}
9595
```
9696

97-
We're just loading static files here :).
97+
We're just loading static files here :).
9898
Between the `<head>` and `</head>`, after the links to the Bootstrap CSS files add this line:
9999

100100
```html
101101
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
102102
```
103-
The browser reads the files in the order they're given, so we need to make sure this is in the right place. Otherwise the code in our file may override code in Bootstrap files.
103+
The browser reads the files in the order they're given, so we need to make sure this is in the right place. Otherwise the code in our file may override code in Bootstrap files.
104104
We just told our template where our CSS file is located.
105105

106106
Your file should now look like this:
@@ -276,10 +276,10 @@ Save those files and refresh your website.
276276

277277
![Figure 14.4](images/final.png)
278278

279-
Woohoo! Looks awesome, right?
280-
Look at the code we just pasted to find the places where we added classes in the HTML and used them in the CSS. Where would you make the change if you wanted the date to be turquoise?
279+
Woohoo! Looks awesome, right?
280+
Look at the code we just pasted to find the places where we added classes in the HTML and used them in the CSS. Where would you make the change if you wanted the date to be turquoise?
281281

282-
Don't be afraid to tinker with this CSS a little bit and try to change some things. Playing with the CSS can help you understand what the different things are doing. If you break something, don't worry, you can always undo it!
282+
Don't be afraid to tinker with this CSS a little bit and try to change some things. Playing with the CSS can help you understand what the different things are doing. If you break something, don't worry, you can always undo it!
283283

284284
We really recommend taking this free online [Codeacademy HTML & CSS course](http://www.codecademy.com/tracks/web). It can help you learn all about making your websites prettier with CSS.
285285

en/deploy/README.md

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ Installing setuptools, pip...done.
167167
168168
$ source myvenv/bin/activate
169169
170-
(myvenv) $ pip install django whitenoise
170+
(myvenv) $ pip install django
171171
Collecting django
172172
[...]
173-
Successfully installed django-1.9 whitenoise-2.0
173+
Successfully installed django-1.9
174174
```
175175

176176

@@ -179,36 +179,6 @@ Successfully installed django-1.9 whitenoise-2.0
179179
<!--TODO: think about using requirements.txt instead of pip install.-->
180180

181181

182-
### Collecting static files.
183-
184-
Were you wondering what the "whitenoise" thing was? It's a tool for serving so-called "static files". Static files are the files that don't regularly change or don't run programming code, such as HTML or CSS files. They work differently on servers compared to on our own computer and we need a tool like "whitenoise" to serve them.
185-
186-
We'll find out a bit more about static files later in the tutorial, when we edit the CSS for our site.
187-
188-
For now we just need to run an extra command called `collectstatic`, on the server. It tells Django to gather up all the static files it needs on the server. At the moment these are mostly files that make the admin site look pretty.
189-
190-
(myvenv) $ python manage.py collectstatic
191-
192-
You have requested to collect static files at the destination
193-
location as specified in your settings:
194-
195-
/home/edith/my-first-blog/static
196-
197-
This will overwrite existing files!
198-
Are you sure you want to do this?
199-
200-
Type 'yes' to continue, or 'no' to cancel: yes
201-
202-
Type "yes", and away it goes! Don't you love making computers print out pages and pages of impenetrable text? I always make little noises to accompany it. Brp, brp brp...
203-
204-
Copying '/home/edith/my-first-blog/myvenv/lib/python3.4/site-packages/django/contrib/admin/static/admin/js/actions.min.js'
205-
Copying '/home/edith/my-first-blog/myvenv/lib/python3.4/site-packages/django/contrib/admin/static/admin/js/inlines.min.js'
206-
[...]
207-
Copying '/home/edith/my-first-blog/myvenv/lib/python3.4/site-packages/django/contrib/admin/static/admin/css/changelists.css'
208-
Copying '/home/edith/my-first-blog/myvenv/lib/python3.4/site-packages/django/contrib/admin/static/admin/css/base.css'
209-
62 static files copied to '/home/edith/my-first-blog/static'.
210-
211-
212182
### Creating the database on PythonAnywhere
213183

214184
Here's another thing that's different between your own computer and the server: it uses a different database. So the user accounts and posts can be different on the server and on your computer.
@@ -227,7 +197,7 @@ We can initialise the database on the server just like we did the one on your ow
227197

228198
## Publishing our blog as a web app
229199

230-
Now our code is on PythonAnywhere, our virtualenv is ready, the static files are collected, and the database is initialised. We're ready to publish it as a web app!
200+
Now our code is on PythonAnywhere, our virtualenv is ready, and the database is initialised. We're ready to publish it as a web app!
231201

232202
Click back to the PythonAnywhere dashboard by clicking on its logo, and go click on the **Web** tab. Finally, hit **Add a new web app**.
233203

@@ -267,14 +237,16 @@ if path not in sys.path:
267237
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
268238

269239
from django.core.wsgi import get_wsgi_application
270-
from whitenoise.django import DjangoWhiteNoise
271-
application = DjangoWhiteNoise(get_wsgi_application())
240+
from django.contrib.staticfiles.handlers import StaticFilesHandler
241+
application = StaticFilesHandler(get_wsgi_application())
272242
```
273243

274244
> **Note** Don't forget to substitute in your own username where it says `<your-username>`
275-
> **Note** In line three, we make sure Pyhthon anywhere knows how to find our application. It is very important that this path name is correct, and especially that there are no extra spaces here. Otherwise you will see an "ImportError" in the error log.
245+
> **Note** In line three, we make sure Python anywhere knows how to find our application. It is very important that this path name is correct, and especially that there are no extra spaces here. Otherwise you will see an "ImportError" in the error log.
246+
247+
This file's job is to tell PythonAnywhere where our web app lives and what the Django settings file's name is.
276248

277-
This file's job is to tell PythonAnywhere where our web app lives and what the Django settings file's name is. It also sets up the "whitenoise" static files tool.
249+
The `StaticFilesHandler` is for dealing with our CSS. This is taken care of automatically for you during local development by the `runserver` command. We'll find out a bit more about static files later in the tutorial, when we edit the CSS for our site.
278250

279251
Hit **Save** and then go back to the **Web** tab.
280252

@@ -285,7 +257,7 @@ We're all done! Hit the big green **Reload** button and you'll be able to go vie
285257

286258
If you see an error when you try to visit your site, the first place to look for some debugging info is in your **error log**. You'll find a link to this on the PythonAnywhere [Web tab](https://www.pythonanywhere.com/web_app_setup/). See if there are any error messages in there; the most recent ones are at the bottom. Common problems include:
287259

288-
- Forgetting one of the steps we did in the console: creating the virtualenv, activating it, installing Django into it, running collectstatic, migrating the database.
260+
- Forgetting one of the steps we did in the console: creating the virtualenv, activating it, installing Django into it, migrating the database.
289261

290262
- Making a mistake in the virtualenv path on the Web tab -- there will usually be a little red error message on there, if there is a problem.
291263

en/django_forms/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,6 @@ $ cd my-first-blog
391391
$ source myvenv/bin/activate
392392
(myvenv)$ git pull
393393
[...]
394-
(myvenv)$ python manage.py collectstatic
395-
[...]
396394
```
397395

398396
* Finally, hop on over to the [Web tab](https://www.pythonanywhere.com/web_app_setup/) and hit **Reload**.

en/extend_your_application/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ $ cd my-first-blog
172172
$ source myvenv/bin/activate
173173
(myvenv)$ git pull
174174
[...]
175-
(myvenv)$ python manage.py collectstatic
176-
[...]
177175
```
178176

179177
* Finally, hop on over to the [Web tab](https://www.pythonanywhere.com/web_app_setup/) and hit **Reload**.

en/html/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,6 @@ $ cd ~/my-first-blog
182182
$ source myvenv/bin/activate
183183
(myvenv)$ git pull
184184
[...]
185-
(myvenv)$ python manage.py collectstatic
186-
[...]
187185
```
188186

189187
And watch your code get downloaded. If you want to check that it's arrived, you can hop over to the **Files tab** and view your code on PythonAnywhere.

0 commit comments

Comments
 (0)