Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions django_admin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@

To add, edit and delete posts we've just modeled, we will use Django admin.

Let's open `mysite/blog/admin.py` file and replace its content with this:
Let's open the `mysite/blog/admin.py` file and replace its content with this:

from django.contrib import admin
from .models import Post

admin.site.register(Post)

As you can see, we import (include) the Post model defined in the previous chapter. To make our model visible on admin page, we need to register the model with `admin.site.register(Post)`.
As you can see, we import (include) the Post model defined in the previous chapter. To make our model visible on the admin page, we need to register the model with `admin.site.register(Post)`.

Ok, time to look at our Post model. Go to the browser and type an address:
Ok, time to look at our Post model. Go to the browser and type the address:

http://127.0.0.1:8000/admin/

You will see a login page like this:

![Login page](images/login_page2.png)

You should use the username and password you chose when you were creating a database (in __Starting Django project__ chapter). After login in, you should see the Django admin dashboard.
You should use the username and password you chose when you were creating a database (in the __Starting Django project__ chapter). After logging in, you should see the Django admin dashboard.

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

Go to Post and experiment a little bit with it. Add five or six blog posts. Don't worry about the content - you can simply copy-paste some text from this tutorial as your posts content to save time :).
Go to Posts and experiment a little bit with it. Add five or six blog posts. Don't worry about the content - you can simply copy-paste some text from this tutorial as your posts' content to save time :).

Make sure that at least two or three posts (but not all) have publish date set. It will be helpful later.
Make sure that at least two or three posts (but not all) have a publish date set. It will be helpful later.

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

Expand Down
34 changes: 17 additions & 17 deletions django_models/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ So basically the idea is to describe real things in code with properties (called

How will we model blog posts then? We want to build a blog, right?

We need to answer a question: what is a blog post? What properties should it have?
We need to answer the question: what is a blog post? What properties should it have?

Well, for sure our blog post needs some text with its content and a title, right? It would be also nice to know who wrote it - we need an author then. Finally, we want to know when the post was created and published.
Well, for sure our blog post needs some text with its content and a title, right? It would be also nice to know who wrote it - so we need an author. Finally, we want to know when the post was created and published.

Post
--------
Expand All @@ -53,11 +53,11 @@ Since we already know what we want to achieve, we can start modelling it in Djan

Knowing what an object is, we can create a Django model for our blog post.

A model in Django is a special kind of object - it is saved in the `database` (database we created in __Database__ chapter).
A model in Django is a special kind of object - it is saved in the `database` (database we created in the __Database__ chapter).

### Creating an application

To keep everything tidy, we will create a separate application inside our `mysite` project. It is very nice to have everything organized from the very beginning. To create an application we need to run in console (from `mysite` folder where `manage.py` file is) `python manage.py startapp blog`.
To keep everything tidy, we will create a separate application inside our `mysite` project. It is very nice to have everything organized from the very beginning. To create an application we need to run in the console (from `mysite` folder where `manage.py` file is) `python manage.py startapp blog`.

(blog) ~/mysite python manage.py startapp blog

Expand All @@ -76,7 +76,7 @@ You will notice that a new `blog` folder is created and it contains a number of
├── tests.py
└── views.py

After creating an application we also need to tell Django that it should use it. We do that in the file `mysite/mysite/setting.py`. We need to find `INSTALLED_APPS` and add a line `blog` just above `)`. We should also add `mysite` application (which was created for us when we started a new project in last chapter). So the final product should look like this:
After creating an application we also need to tell Django that it should use it. We do that in the file `mysite/mysite/settings.py`. We need to find `INSTALLED_APPS` and add a line `blog` just above `)`. We should also add the `mysite` application (which was created for us when we started a new project in the last chapter). So the final product should look like this:

INSTALLED_APPS = (
'django.contrib.admin',
Expand Down Expand Up @@ -117,32 +117,32 @@ Let's open `mysite/blog/models.py`, remove everything from it and write code lik

It is scary, right? But no worries, we will explain what these lines mean!

All lines started with `from` or `import` are lines that adds some bits from other files. So instead of copying and pasting the same things in every file, we can include some parts with `from ... import ...`.
All lines starting with `from` or `import` are lines that add some bits from other files. So instead of copying and pasting the same things in every file, we can include some parts with `from ... import ...`.

`class Post(models.Model):` - this line defines our model (it is an `object`!).

- `class` is a special keyword that indicates that we are defining an object.
- `Post` is the name of our model, we can call it differently (but we have to avoid special characters and whitespaces). Always start a name with uppercase.
- `models.Model` means that the Post is a Django Model, so the Django knows that it should be saved in the database.
- `Post` is the name of our model, we can give it a different name (but we must avoid special characters and whitespaces). Always start a name with an uppercase letter.
- `models.Model` means that the Post is a Django Model, so Django knows that it should be saved in the database.

Now we define properties we were talking about: `title`, `text`, `created_date`, `published_date` and `author`. To do that we need to define a type of field (is it a text? a number? date? a relation to another object, i.e. a User?).
Now we define properties we were talking about: `title`, `text`, `created_date`, `published_date` and `author`. To do that we need to define a type of field (is it text? A number? A date? A relation to another object, i.e. a User?).

- `models.CharField` - this is how you define text with limited number of characters.
- `models.CharField` - this is how you define text with a limited number of characters.
- `models.TextField` - this is for long texts without a limit. It will be ideal for a blog post content, right?
- `models.DateTimeField` - this is a date and time
- `models.ForeignKey` - this is a link to another model
- `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 things described above (https://docs.djangoproject.com/en/1.6/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/1.6/ref/models/fields/#field-types).

What about `def publish(self):`? It is exactly our `publish` method we were talking about. `def` means that this is a function/method. `publish` is a name of the method. You can change it, if you want. The rule is that we use lowercase and underscores instead of whitespaces (i.e. if you want to have method that calculates average price you could call it `calculate_average_price`).
What about `def publish(self):`? It is exactly our `publish` method we were talking about before. `def` means that this is a function/method. `publish` is a name of the method. You can change it, if you want. The rule is that we use lowercase and underscores instead of whitespaces (i.e. if you want to have method that calculates average price you could call it `calculate_average_price`).

Methods very often `return` something. There is an example of that in the `__str__` method. In this scenario, when we call `__str__()` we will get a text (**string**) with a Post title.

If something is still not clear about models, feel free to ask your coach! We know, it is very complicated, especially when you learn what objects and functions are at the same time. But hopefully, it looks sligthly less magic for you now!
If something is still not clear about models, feel free to ask your coach! We know it is very complicated, especially when you learn what objects and functions are at the same time. But hopefully it looks sligthly less magic for you now!

### Create tables for models in your database

Last step here is to add our new model to our database. It is as easy as typing `python manage.py syncdb`. It will look like this:
The last step here is to add our new model to our database. It is as easy as typing `python manage.py syncdb`. It will look like this:

(blog) ~/mysite python manage.py syncdb
Creating tables ...
Expand All @@ -151,7 +151,7 @@ Last step here is to add our new model to our database. It is as easy as typing
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

It would be nice to see this Post model, right? Jump to the next chapter to see how your Post look like!
It would be nice to see this Post model, right? Jump to the next chapter to see what your Post looks like!



28 changes: 14 additions & 14 deletions django_urls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

We're about to build our first webpage -- a homepage for your blog! But first, let's learn a little bit about Django urls.

## What is URL?
## What is a URL?

A URL is simply like a web address, you can see an URL every time you visit any website - it is visible in your browser's address bar (yes! `127.0.0.1:8000` is an URL! And http://djangogirls.com is also an URL):
A URL is simply like a web address, you can see a URL every time you visit any website - it is visible in your browser's address bar (yes! `127.0.0.1:8000` is a URL! And http://djangogirls.com is also a URL):

![Url](images/url.png)

Every page on the Internet needs its own URL. This way your application knows what it should show to a user who opens an URL. In Django we use something called `URLconf` (URL configuration), which is a set of patterns that Django will try to match with the received URL to find the correct view.
Every page on the Internet needs its own URL. This way your application knows what it should show to a user who opens a URL. In Django we use something called `URLconf` (URL configuration), which is a set of patterns that Django will try to match with the received URL to find the correct view.

## How URLs work in Django?
## How do URLs work in Django?

Let's open up the `mysite/mysite/urls.py` file and see how it looks like:
Let's open up the `mysite/mysite/urls.py` file and see what it looks like:

from django.conf.urls import patterns, include, url

Expand All @@ -27,9 +27,9 @@ Let's open up the `mysite/mysite/urls.py` file and see how it looks like:
url(r'^admin/', include(admin.site.urls)),
)

As you can see, Django already put something for us here.
As you can see, Django already put something here for us.

Lines that starts with `#` are comments - it means that those lines won't be executed by Python. Pretty handy, right?
Lines that start with `#` are comments - it means that those lines won't be executed by Python. Pretty handy, right?

The admin url, which you visited in previous chapter is already here:

Expand All @@ -39,14 +39,14 @@ It means that for every url that starts with `admin/` Django will find a corresp

## Regex

Do you wonder how Django matches URLs to views? Well, this part is tricky. Django uses `regex` -- regular expressions. Regex has a lot (a lot!) of rules that forms a search pattern. It is not so easy to understand so we won't worry about it today and you'll definitely get to know them in the future. Today we will only use the ones we need.
Do you wonder how Django matches URLs to views? Well, this part is tricky. Django uses `regex` -- regular expressions. Regex has a lot (a lot!) of rules that form a search pattern. It is not so easy to understand so we won't worry about it today and you'll definitely get to know them in the future. Today we will only use the ones we need.

## Your first Django url!

Time to create our first urls! We want http://127.0.0.1:8000/ to be a homepage of our blog and display a list of posts.

We also want to keep `mysite/mysite/urls.py` file clean, so we will import urls from our `blog` application to main `mysite/mysite/urls.py` file.
Go ahead, delete comment lines (lines started with `#`) and add a line that will import `blog.urls` into main url (`''`).
We also want to keep the `mysite/mysite/urls.py` file clean, so we will import urls from our `blog` application to the main `mysite/mysite/urls.py` file.
Go ahead, delete comment lines (lines starting with `#`) and add a line that will import `blog.urls` into the main url (`''`).

Your `mysite/mysite/urls.py` file should now look like this:

Expand Down Expand Up @@ -77,11 +77,11 @@ After that, we can add our first URL pattern:
url(r'^$', views.post_list),
)

As you can see, we're now assigning `view` called `post_list` to `^$` URL. But what `^$` means? It's regex magic :) Let's break it down:
- `^` in regex means "the beginning", from this sign we can start looking for our pattern
- `$` matches only "the end" of the string, which means that we will finish looking for our pattern here.
As you can see, we're now assigning a `view` called `post_list` to `^$` URL. But what does `^$` mean? It's regex magic :) Let's break it down:
- `^` in regex means "the beginning"; from this sign we can start looking for our pattern
- `$` matches only "the end" of the string, which means that we will finish looking for our pattern here

If you put this two signs together, it looks like we're looking for empty string! And that's correct, because in Django url resolvers, `http://127.0.0.1:8000/` is not a part of URL. This pattern will show Django that `views.post_list` is the right place to go if someone enter your website on `http://127.0.0.1:8000/` address.
If you put these two signs together, it looks like we're looking for an empty string! And that's correct, because in Django url resolvers, `http://127.0.0.1:8000/` is not a part of URL. This pattern will show 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.

Everything all right? Open http://127.0.0.1:8000/ in your browser to see the result.

Expand Down
14 changes: 7 additions & 7 deletions django_views/README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
# Django views - time to create!

Time to get rid of the bug we created in last chapter :)
Time to get rid of the bug we created in the last chapter :)

A *view* is a place where we put the "logic" of our application. It will request information from the `model` you created before and pass it to a `template` that you will create in next chapter. Views are just Python methods that are a little bit more complicated than the thing we did in __Introduction to Python__ chapter.
A *view* is a place where we put the "logic" of our application. It will request information from the `model` you created before and pass it to a `template` that you will create in the next chapter. Views are just Python methods that are a little bit more complicated than the thing we did in the __Introduction to Python__ chapter.

Views are placed in `views.py` file. We will add our *views* to `mysite/blog/views.py` file.
Views are placed in the `views.py` file. We will add our *views* to the `mysite/blog/views.py` file.

## blog/views.py

Ok, let's open up this file and see what's in there:
OK, let's open up this file and see what's in there:

from django.shortcuts import render

# Create your views here.

Not too much stuff here yet. The simplest *view* can look like that.
Not too much stuff here yet. The simplest *view* can look like this.

def post_list(request):

return render(request, 'blog/post_list.html', {})

As you can see, we created a method (`def`) called `post_list` that takes `request` and `return` a method `render` that will render (put together) our template `blog/post_list.html`.

Save the file, go to http://127.0.0.1:8000/ and see what we got now.
Save the file, go to http://127.0.0.1:8000/ and see what we have got now.

Another error! Read what's going on now:

![Error](images/error.png)

This one is easy: *TemplateDoesNotExist*. Let's fix this bug and create a template in next chapter!
This one is easy: *TemplateDoesNotExist*. Let's fix this bug and create a template in the next chapter!