Skip to content

Commit

Permalink
Merge pull request DjangoGirls#1282 from pyladiesdf/master
Browse files Browse the repository at this point in the history
[EN] Convert Django 1.11 to 2.0
  • Loading branch information
magul authored Jul 17, 2018
2 parents 38aa6cb + b11e448 commit f1df6b6
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 69 deletions.
3 changes: 3 additions & 0 deletions book.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"gitbook": ">=3.2.0",
"variables": {
"django_version": "2.0.6"
},
"links": {
"sidebar": {
"Need help? Talk to us!": "https://gitter.im/DjangoGirls/tutorial"
Expand Down
2 changes: 1 addition & 1 deletion en/chromebook_setup/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mkdir djangogirls
cd djangogirls
python3.6 -mvenv myvenv
source myvenv/bin/activate
pip install django~=1.11.0
pip install django~={{ book.django_version }}
```

(note that on the last line we use a tilde followed by an equal sign: ~=).
Expand Down
2 changes: 1 addition & 1 deletion en/deploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ You can also go to the "Files" tab and navigate around using PythonAnywhere's bu
Your site should now be live on the public Internet! Click through to the PythonAnywhere "Web" tab to get a link to it. You can share this with anyone you want :)


> **Note** This is a beginners' tutorial, and in deploying this site we've taken a few shortcuts which aren't ideal from a security point of view. If and when you decide to build on this project, or start a new project, you should review the [Django deployment checklist](https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/) for some tips on securing your site.
> **Note** This is a beginners' tutorial, and in deploying this site we've taken a few shortcuts which aren't ideal from a security point of view. If and when you decide to build on this project, or start a new project, you should review the [Django deployment checklist](https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/) for some tips on securing your site.

## Debugging tips
Expand Down
2 changes: 1 addition & 1 deletion en/django_admin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ Make sure that at least two or three posts (but not all) have the publish date s

![Django admin](images/edit_post3.png)

If you want to know more about Django admin, you should check Django's documentation: https://docs.djangoproject.com/en/1.11/ref/contrib/admin/
If you want to know more about Django admin, you should check Django's documentation: https://docs.djangoproject.com/en/2.0/ref/contrib/admin/

This is probably a good moment to grab a coffee (or tea) or something to eat to re-energize yourself. You created your first Django model – you deserve a little break!
14 changes: 7 additions & 7 deletions en/django_forms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,20 @@ We open `blog/urls.py` and add a line:

{% filename %}blog/urls.py{% endfilename %}
```python
url(r'^post/new/$', views.post_new, name='post_new'),
path('post/new', views.post_new, name='post_new'),
```

And the final code will look like this:

{% filename %}blog/urls.py{% endfilename %}
```python
from django.conf.urls import url
from django.urls import path
from . import views

urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
url(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
url(r'^post/new/$', views.post_new, name='post_new'),
path('', views.post_list, name='post_list'),
path('post/<int:pk>/', views.post_detail, name='post_detail'),
path('post/new/', views.post_new, name='post_new'),
]
```

Expand Down Expand Up @@ -302,7 +302,7 @@ In `blog/urls.py` we add this line:

{% filename %}blog/urls.py{% endfilename %}
```python
url(r'^post/(?P<pk>\d+)/edit/$', views.post_edit, name='post_edit'),
path('post/<int:pk>/edit/', views.post_edit, name='post_edit'),
```

We will reuse the template `blog/templates/blog/post_edit.html`, so the last missing thing is a *view*.
Expand Down Expand Up @@ -352,7 +352,7 @@ Feel free to change the title or the text and save the changes!

Congratulations! Your application is getting more and more complete!

If you need more information about Django forms, you should read the documentation: https://docs.djangoproject.com/en/1.11/topics/forms/
If you need more information about Django forms, you should read the documentation: https://docs.djangoproject.com/en/2.0/topics/forms/

## Security

Expand Down
33 changes: 26 additions & 7 deletions en/django_installation/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,34 @@ Before we do that, we should make sure we have the latest version of `pip`, the
(myvenv) ~$ python3 -m pip install --upgrade pip
```
Then run `pip install django~=1.11.0` (note that we use a tilde followed by an equal sign: `~=`) to install Django.
### Installing packages with requirements
A requirements file keeps a list of dependencies to be installed using
`pip install`:
First create a `requirements.txt` file inside of `djangogirls/` folder:
```
djangogirls
└───requirements.txt
```
In your `djangogirls/requirements.txt` file you should add the following text:
{% filename %}djangogirls/requirements.txt{% endfilename %}
```
Django=={{ book.django_version }}
```
Now, run `pip install -r requirements.txt` to install Django.
{% filename %}command-line{% endfilename %}
```
(myvenv) ~$ pip install django~=1.11.0
Collecting django~=1.11.0
Downloading Django-1.11.3-py2.py3-none-any.whl (6.8MB)
Installing collected packages: django
Successfully installed django-1.11.3
(myvenv) ~$ pip install -r requirements.txt
Collecting Django=={{ book.django_version }} (from -r requirements.txt (line 1))
Downloading Django-{{ book.django_version }}-py3-none-any.whl (7.1MB)
Installing collected packages: Django
Successfully installed Django-{{ book.django_version }}
```
<!--sec data-title="Installing Django: Windows" data-id="django_err_windows"
Expand All @@ -192,7 +211,7 @@ data-collapse=true ces-->
>
>{% filename %}command-line{% endfilename %}
>```
>C:\Users\Name\djangogirls> python -m pip install django~=1.11.0
>C:\Users\Name\djangogirls> python -m pip install -r requirements.txt
>```
<!--endsec-->
Expand Down
2 changes: 1 addition & 1 deletion en/django_models/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ Now we define the properties we were talking about: `title`, `text`, `created_da
- `models.DateTimeField` – this is a date and time.
- `models.ForeignKey` – this is a link to another model.

We will not explain every bit of code here since it would take too much time. You should take a look at Django's documentation if you want to know more about Model fields and how to define things other than those described above (https://docs.djangoproject.com/en/1.11/ref/models/fields/#field-types).
We will not explain every bit of code here since it would take too much time. You should take a look at Django's documentation if you want to know more about Model fields and how to define things other than those described above (https://docs.djangoproject.com/en/2.0/ref/models/fields/#field-types).

What about `def publish(self):`? This is exactly the `publish` method we were talking about before. `def` means that this is a function/method and `publish` is the name of the method. You can change the name of the method if you want. The naming rule is that we use lowercase and underscores instead of spaces. For example, a method that calculates average price could be called `calculate_average_price`.

Expand Down
4 changes: 2 additions & 2 deletions en/django_start_project/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ In `settings.py`, find the line that contains `TIME_ZONE` and modify it to choos
TIME_ZONE = 'Europe/Berlin'
```

A language code consist of the language, e.g. `en` for English or `de` for German, and the country code, e.g. `de` for Germany or `ch` for Switzerland. If English is not your native language, you can can add this to change the default buttons and notifications from Django to be in your language. So you would have "Cancel" button translated into the language you defined here. [Django comes with a lot of prepared translations](https://docs.djangoproject.com/en/1.11/ref/settings/#language-code).
A language code consist of the language, e.g. `en` for English or `de` for German, and the country code, e.g. `de` for Germany or `ch` for Switzerland. If English is not your native language, you can can add this to change the default buttons and notifications from Django to be in your language. So you would have "Cancel" button translated into the language you defined here. [Django comes with a lot of prepared translations](https://docs.djangoproject.com/en/2.0/ref/settings/#language-code).

If you want a different language, change the language code by changing the following line:

Expand Down Expand Up @@ -196,7 +196,7 @@ https://django-girls-<your cloud9 username>.c9users.io

Congratulations! You've just created your first website and run it using a web server! Isn't that awesome?

![It worked!](images/it_worked2.png)
![Install worked!](images/install_worked.png)

While the web server is running, you won't see a new command-line prompt to enter additional commands. The terminal will accept new text but will not execute new commands. This is because the web server continuously runs in order to listen for incoming requests.

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed en/django_start_project/images/it_worked2.png
Binary file not shown.
47 changes: 10 additions & 37 deletions en/django_urls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ Let's open up the `mysite/urls.py` file in your code editor of choice and see wh
[...]
"""
from django.conf.urls import url
from django.urls import path, include
from django.contrib import admin

urlpatterns = [
url(r'^admin/', admin.site.urls),
path('admin/', admin.site.urls),
]
```

Expand All @@ -37,34 +37,11 @@ The admin URL, which you visited in the previous chapter, is already here:

{% filename %}mysite/urls.py{% endfilename %}
```python
url(r'^admin/', admin.site.urls),
path('admin/', admin.site.urls),
```

This line means that for every URL that starts with `admin/`, Django will find a corresponding *view*. In this case, we're including a lot of admin URLs so it isn't all packed into this small file – it's more readable and cleaner.

## Regex

Do you wonder how Django matches URLs to views? Well, this part is tricky. Django uses `regex`, short for "regular expressions". Regex has a lot (a lot!) of rules that form a search pattern. Since regexes are an advanced topic, we will not go into detail over how they work.

If you still wish to understand how we created the patterns, here is an example of the process – we will only need a limited subset of the rules to express the pattern we are looking for, namely:

* `^` for the beginning of the text
* `$` for the end of the text
* `\d` for a digit
* `+` to indicate that the previous item should be repeated at least once
* `()` to capture part of the pattern

Anything else in the URL definition will be taken literally.

Now imagine you have a website with the address like `http://www.mysite.com/post/12345/`, where `12345` is the number of your post.

Writing separate views for all the post numbers would be really annoying. With regular expressions, we can create a pattern that will match the URL and extract the number for us: `^post/(\d+)/$`. Let's break this down piece by piece to see what we are doing here:

* **^post/** is telling Django to take anything that has `post/` at the beginning of the URL (right after `^`)
* **(\d+)** means that there will be a number (one or more digits) and that we want the number captured and extracted
* **/** tells Django that another `/` character should follow
* **$** then indicates the end of the URL meaning that only strings ending with the `/` will match this pattern


## Your first Django URL!

Expand All @@ -78,28 +55,24 @@ Your `mysite/urls.py` file should now look like this:

{% filename %}mysite/urls.py{% endfilename %}
```python
from django.conf.urls import include
from django.conf.urls import url
from django.urls import path, include
from django.contrib import admin

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('blog.urls')),
path('admin/', admin.site.urls),
path('', include('blog.urls')),
]
```

Django will now redirect everything that comes into 'http://127.0.0.1:8000/' to `blog.urls` and looks for further instructions there.

Writing regular expressions in Python is always done with `r` in front of the string. This is a helpful hint for Python that the string may contain special characters that are not meant for Python itself, but for the regular expression instead.


## blog.urls

Create a new empty file named `urls.py` in the `blog` directory. All right! Add these first two lines:

{% filename %}blog/urls.py{% endfilename %}
```python
from django.conf.urls import url
from django.urls import path
from . import views
```

Expand All @@ -110,11 +83,11 @@ After that, we can add our first URL pattern:
{% filename %}blog/urls.py{% endfilename %}
```python
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
path('', views.post_list, name='post_list'),
]
```

As you can see, we're now assigning a `view` called `post_list` to the `^$` URL. This regular expression will match `^` (a beginning) followed by `$` (an end) – so only an empty string will match. That's correct, because in Django URL resolvers, 'http://127.0.0.1:8000/' is not a part of the URL. This pattern will tell Django that `views.post_list` is the right place to go if someone enters your website at the 'http://127.0.0.1:8000/' address.
As you can see, we're now assigning a `view` called `post_list` to the root URL. This URL pattern will match an empty string and the Django URL resolver will ignore the domain name (i.e., http://127.0.0.1:8000/) that prefixes the full url path. This pattern will tell Django that `views.post_list` is the right place to go if someone enters your website at the 'http://127.0.0.1:8000/' address.

The last part, `name='post_list'`, is the name of the URL that will be used to identify the view. This can be the same as the name of the view but it can also be something completely different. We will be using the named URLs later in the project, so it is important to name each URL in the app. We should also try to keep the names of URLs unique and easy to remember.

Expand All @@ -124,4 +97,4 @@ If you try to visit http://127.0.0.1:8000/ now, then you'll find some sort of 'w

Your console is showing an error, but don't worry – it's actually pretty useful: It's telling you that there is __no attribute 'post_list'__. That's the name of the *view* that Django is trying to find and use, but we haven't created it yet. At this stage, your `/admin/` will also not work. No worries – we will get there.

> If you want to know more about Django URLconfs, look at the official documentation: https://docs.djangoproject.com/en/1.11/topics/http/urls/
> If you want to know more about Django URLconfs, look at the official documentation: https://docs.djangoproject.com/en/2.0/topics/http/urls/
2 changes: 1 addition & 1 deletion en/django_views/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ Another error! Read what's going on now:

This shows that the server is running again, at least, but it still doesn't look right, does it? Don't worry, it's just an error page, nothing to be scared of! Just like the error messages in the console, these are actually pretty useful. You can read that the *TemplateDoesNotExist*. Let's fix this bug and create a template in the next chapter!

> Learn more about Django views by reading the official documentation: https://docs.djangoproject.com/en/1.11/topics/http/views/
> Learn more about Django views by reading the official documentation: https://docs.djangoproject.com/en/2.0/topics/http/views/
2 changes: 1 addition & 1 deletion en/dynamic_data_in_templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ def post_list(request):

That's it! Time to go back to our template and display this QuerySet!

Want to read a little bit more about QuerySets in Django? You should look here: https://docs.djangoproject.com/en/1.11/ref/models/querysets/
Want to read a little bit more about QuerySets in Django? You should look here: https://docs.djangoproject.com/en/2.0/ref/models/querysets/
16 changes: 7 additions & 9 deletions en/extend_your_application/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,23 @@ Let's create a URL in `urls.py` for our `post_detail` *view*!

We want our first post's detail to be displayed at this **URL**: http://127.0.0.1:8000/post/1/

Let's make a URL in the `blog/urls.py` file to point Django to a *view* named `post_detail`, that will show an entire blog post. Add the line `url(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),` to the `blog/urls.py` file. The file should look like this:
Let's make a URL in the `blog/urls.py` file to point Django to a *view* named `post_detail`, that will show an entire blog post. Add the line `path('post/<int:pk>)/', views.post_detail, name='post_detail'),` to the `blog/urls.py` file. The file should look like this:

{% filename %}{{ warning_icon }} blog/urls.py{% endfilename %}
```python
from django.conf.urls import url
from . import views

urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
url(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
path('', views.post_list, name='post_list'),
path('post/<int:pk>/', views.post_detail, name='post_detail'),
]
```

This part ``^post/(?P<pk>\d+)/$`` looks scary, but no worries – we will explain it for you:
- it starts with `^` again – "the beginning".
- `post/` just means that after the beginning, the URL should contain the word __post__ and a __/__. So far so good.
- `(?P<pk>\d+)` – this part is trickier. It means that Django will take everything that you place here and transfer it to a view as a variable called `pk`. (Note that this matches the name we gave the primary key variable back in `blog/templates/blog/post_list.html`!) `\d` also tells us that it can only be a digit, not a letter (so everything between 0 and 9). `+` means that there needs to be one or more digits there. So something like `http://127.0.0.1:8000/post//` is not valid, but `http://127.0.0.1:8000/post/1234567890/` is perfectly OK!
- `/` – then we need a __/__ again.
- `$` – "the end"!
This part ``post/<int:pk>/`` specifies a URL pattern – we will explain it for you:
- `post/` just means that the URL should begin with the word __post__ followed by a __/__. So far so good.
- `<int:pk>` – this part is trickier. It means that Django expects an integer value and will transfer it to a view as a variable called `pk`.
- `/` – then we need a __/__ again before finishing the url.

That means if you enter `http://127.0.0.1:8000/post/5/` into your browser, Django will understand that you are looking for a *view* called `post_detail` and transfer the information that `pk` equals `5` to that *view*.

Expand Down
2 changes: 1 addition & 1 deletion en/whats_next/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ After that, make sure to follow Django Girls on [Facebook](http://facebook.com/d
Yes! First, go ahead and try our other book, called [Django Girls Tutorial: Extensions](https://tutorial-extensions.djangogirls.org/).

Later on, you can try the resources listed below. They're all very recommended!
- [Django's official tutorial](https://docs.djangoproject.com/en/1.11/intro/tutorial01/)
- [Django's official tutorial](https://docs.djangoproject.com/en/2.0/intro/tutorial01/)
- [New Coder tutorials](http://newcoder.io/tutorials/)
- [Code Academy Python course](https://www.codecademy.com/en/tracks/python)
- [Code Academy HTML & CSS course](https://www.codecademy.com/tracks/web)
Expand Down

0 comments on commit f1df6b6

Please sign in to comment.