Skip to content

Commit

Permalink
Merge pull request #49 from BluesFiend/master
Browse files Browse the repository at this point in the history
Folder structure rearrange
  • Loading branch information
oinopion committed Jul 26, 2014
2 parents bfb55d4 + bf08ac1 commit 68c7ec1
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 109 deletions.
31 changes: 15 additions & 16 deletions css/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ It was written by programmers who worked for Twitter and is now developed by vol

## Install Boostrap

To install Bootstrap, you need to add this to your `<head>` in you `.html` file (`mysite/blog/templates/blog/post_list.html`):
To install Bootstrap, you need to add this to your `<head>` in you `.html` file (`blog/templates/blog/post_list.html`):

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
Expand All @@ -36,12 +36,12 @@ CSS is a static file, so in order to customize CSS, we need to first configure s

### Configure static files in Django

First, we need to create a folder to store our static files in. Go ahead and create a folder called `static` inside your (parent) `mysite` folder.
First, we need to create a folder to store our static files in. Go ahead and create a folder called `static` inside your `djangogirls` folder.

mysite
└───static
static
manage.py

Now we need to tell Django how it can find it. Open up the `mysite/mysite/settings.py` file, scroll to the bottom of it and add the following lines:
Now we need to tell Django how it can find it. Open up the `mysite/settings.py` file, scroll to the bottom of it and add the following lines:

STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
Expand All @@ -55,26 +55,25 @@ That's it! Time to see if it works :)

First things first: let's create a CSS file now. Create a new folder called `css` inside your `static` folder. Then create a new file called `blog.css` inside this `css` directory. Ready?

mysite
└───static
└───css
blog.css
static
└───css
blog.css

Time to write some CSS! Open up the `mysite/static/css/blog.css` file in your code editor.
Time to write some CSS! Open up the `static/css/blog.css` file in your code editor.

We won't be going too deep into customizing and learning about CSS here, because it's pretty easy and you can learn it on your own after this workshop. We really recommend doing this [Codeacademy HTML & CSS course](http://www.codecademy.com/tracks/web) to learn everything you need to know about making your websites more pretty with CSS.

But let's do at least a little. Maybe we could change the color of our header? To understand colors, computer use special codes. They start with `#` and are followed by 6 letters (A-F) and numbers (0-9). You can find color codes for example here: http://www.colorpicker.com/

In your `mysite/static/css/blog.css` file you should add following code:
In your `static/css/blog.css` file you should add following code:

h1 a {
color: #FCA205;
}

`a` inside of `h1` (i.e. when we have in code something like: `<h1><a href="">link</a></h1>`) is the tag we're applying our styles to, and we're telling it to change color to `#FCA205`. Of course, you can put your own color here!

Then, we need to also tell our HTML template that we added some CSS. Open the `mysite/blog/templates/blog/post_list.html` file and add this line at the very beginning of it:
Then, we need to also tell our HTML template that we added some CSS. Open the `blog/templates/blog/post_list.html` file and add this line at the very beginning of it:

{% load staticfiles %}

Expand Down Expand Up @@ -122,13 +121,13 @@ Add this to your CSS, save the file and see how it works!

![Figure 14.3](images/margin2.png)

Maybe we can customize the font in our header? Paste this into your `<head>` in `mysite/blog/templates/blog/post_list.html` file:
Maybe we can customize the font in our header? Paste this into your `<head>` in `blog/templates/blog/post_list.html` file:

<link href="http://fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">

This line will import a font called *Lobster* from Google Fonts (https://www.google.com/fonts).

Now add this line in `mysite/static/css/blog.css` and refresh the page:
Now add this line in `static/css/blog.css` and refresh the page:

h1 a {
color: #FCA205;
Expand All @@ -155,7 +154,7 @@ And now add a class `post` to your `div` containing blogposts.
<p>{{ post.text }}</p>
</div>

All right. We have only one day, so we need to speed things up a little! We can't explain you every little detail about CSS. For now just copy and paste following code into your `mysite/static/css/blog.css` file:
All right. We have only one day, so we need to speed things up a little! We can't explain you every little detail about CSS. For now just copy and paste following code into your `static/css/blog.css` file:

.page-header {
background-color: #ff9400;
Expand Down Expand Up @@ -215,7 +214,7 @@ Then also replace this:
</div>
{% endfor %}

in the `mysite/blog/templates/blog/post_list.html` with this:
in the `blog/templates/blog/post_list.html` with this:

<div class="content">
<div class="row">
Expand Down
16 changes: 8 additions & 8 deletions deploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ We need to create a `requirements.txt` file to tell Heroku what Python packages

But first, Heroku needs us to install the `django-toolbelt` package. Go to your console with `virtualenv` activated and type this:

(blog) $ pip install django-toolbelt
(venv) $ pip install django-toolbelt

After the installation is finished, run this command:

(blog) $ pip freeze > requirements.txt
(venv) $ pip freeze > requirements.txt

This will create a file called `requirements.txt` with a list of your installed packages (i.e. Python libraries that you are using, for example Django :)).

Expand All @@ -38,7 +38,7 @@ We need to tell Heroku which Python version we want to use. This is simply done

There is a difference between settings we are using locally (on our computer) and settings for our server. Heroku is using one database, and your computer is using a different database. That's why we need to create a seperate file for settings that will only be available for our local enviroment.

Go ahead and create `mysite/mysite/local_settings.py` file. It should contain your `DATABASE` setup from your `mysite/mysite/settings.py` file. Just like that:
Go ahead and create `mysite/local_settings.py` file. It should contain your `DATABASE` setup from your `mysite/settings.py` file. Just like that:

DATABASES = {
'default': {
Expand All @@ -55,7 +55,7 @@ Then just save it! :)

## mysite/settings.py

Another thing we need to do is modify our website's `settings.py` file. Open `mysite/mysite/settings.py` in your editor and add the following lines at the end of the file:
Another thing we need to do is modify our website's `settings.py` file. Open `mysite/settings.py` in your editor and add the following lines at the end of the file:

import dj_database_url
DATABASES['default'] = dj_database_url.config()
Expand All @@ -66,7 +66,7 @@ Another thing we need to do is modify our website's `settings.py` file. Open `my

STATIC_ROOT = 'staticfiles'

At the end of the `mysite/mysite/settings.py`, copy and paste this:
At the end of the `mysite/settings.py`, copy and paste this:

try:
from .local_settings import *
Expand All @@ -79,7 +79,7 @@ Then save the file.

## mysite/urls.py

Open `mysite/mysite/urls.py` file and add these two lines in the beginning of the file:
Open `mysite/urls.py` file and add these two lines in the beginning of the file:

from django.conf.urls.static import static
from django.conf import settings
Expand All @@ -104,7 +104,7 @@ The whole thing should look like this:

## mysite/wsgi.py

Open the `mysite/mysite/wsgi.py` file and replace this line:
Open the `mysite/wsgi.py` file and replace this line:

application = get_wsgi_application()

Expand Down Expand Up @@ -135,7 +135,7 @@ In case you don't have SSH key that command would automatically create one. SSH

Heroku uses git repository to manage your project files, so we need to use it to.

Create `mysite/.gitignore` file with following content:
Create `.gitignore` file with following content:

venv
__pycache__
Expand Down
2 changes: 1 addition & 1 deletion django_admin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

from django.contrib import admin
from .models import Post
Expand Down
25 changes: 12 additions & 13 deletions django_forms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ This is exactly what we want to do: we will create a form for our `Post` model.

Like every important part of Django, forms have their own file: `forms.py`.

We need to create a file with this name in the `mysite/blog` folder.
We need to create a file with this name in the `blog` folder.

mysite
└── blog
forms.py
blog
└── forms.py

Ok, let's open it and type the following code:

Expand Down Expand Up @@ -40,7 +39,7 @@ So once again we will create: a link to the page, an URL, a view and a template.

## Link to page with the form

It's time to open `mysite/mysite/templates/mysite/base.html`. We will add a link in `div` named `page-header`:
It's time to open `mysite/templates/mysite/base.html`. We will add a link in `div` named `page-header`:

<a href="{% url 'blog.views.post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>

Expand Down Expand Up @@ -80,7 +79,7 @@ After saving and refreshing the page `http://127.0.0.1:8000` you will obviously

## URL

We open `mysite/blog/urls.py` and add a line:
We open `blog/urls.py` and add a line:

url(r'^post/new/$', views.post_new),

Expand All @@ -99,7 +98,7 @@ After refreshing the site, we see an `AttributeError`, since we don't have `post

## post_new view

Time to open the `mysite/blog/views.py` file and add the following lines:
Time to open the `blog/views.py` file and add the following lines:

from .forms import PostForm

Expand All @@ -113,7 +112,7 @@ To create a new `Post` form, we need to call `PostForm()` and pass it to the tem

## Template

We need to create a file `post_edit.html` in the `mysite/blog/template/blog` directory. To make a form work we need several things:
We need to create a file `post_edit.html` in the `blog/template/blog` directory. To make a form work we need several things:

- we have to display the form. We can do that for example with a `{{ form.as_p }}`.
- the line above have to be wrapped with HTML form tag: <`form method="POST">...</form>`
Expand Down Expand Up @@ -146,7 +145,7 @@ The answer is: nothing. We need to do a little bit more work in our *view*.

## Saving the form

Open `mysite/blog/views.py` once again. Currently all we have in `post_new` view is:
Open `blog/views.py` once again. Currently all we have in `post_new` view is:

def post_new(request):
form = PostForm()
Expand Down Expand Up @@ -220,7 +219,7 @@ Django is taking care of validating that all fields in our form are correct. Isn

Now we know how to add a new form. But what if we want to edit an existing one? It is very similar to the thing we just did. Let's create some important things quickly (if you don't understand something - you should ask your coach or look at the previous chapters, since we covered all the steps already).

Open `mysite/blog/post_detail.html` and add this line:
Open `blog/post_detail.html` and add this line:

<a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a>

Expand All @@ -239,13 +238,13 @@ so that the template will look like:
<p>{{ post.text }}</p>
{% endblock %}

In `mysite/blog/urls.py` we add this line:
In `blog/urls.py` we add this line:

url(r'^post/(?P<pk>[0-9]+)/edit/$', views.post_edit, name='post_edit'),

We will reuse the template `mysite/blog/templates/blog/post_edit.html`, so the last missing thing is a *view*.
We will reuse the template `blog/templates/blog/post_edit.html`, so the last missing thing is a *view*.

Let's open a `mysite/blog/views.py` and add at the very end of the file:
Let's open a `blog/views.py` and add at the very end of the file:

def post_edit(request, pk):
post = get_object_or_404(Post, pk=pk)
Expand Down
30 changes: 18 additions & 12 deletions django_installation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,47 @@ So, let's create a **virtual environment** (also called a *virtualenv*). It will

All you need to do is find a folder in which you want to create the `virtualenv`; your home directory, for example. On Windows it might look like `C:\Users\Name\` (where `Name` is the name of your login).

For this tutorial we will be using a new directory `djangogirls` from your home directory:

mkdir djangogirls

We will make a virtualenv called `venv`. The general command will be in the format python -m venv `name_of_venv`.

### Windows

To create a new `virtualenv`, you need to open the console (we told you about that a few tutorials ago - remember?) and run `C:\Python\python -m venv blog`. It will look like this:
To create a new `virtualenv`, you need to open the console (we told you about that a few tutorials ago - remember?) and run `C:\Python\python -m venv venv`. It will look like this:

C:\Users\Name> C:\Python34\python -m venv blog
C:\Users\Name\djangogirls> C:\Python34\python -m venv venv

where `C:\Python34\python` is the folder in which you previously installed Python and `blog` is a name of your `virtualenv`. You can use any other name, but stick to lowercase and use no spaces. It is also good idea to keep the name short - you'll be referencing it a lot!
where `C:\Python34\python` is the folder in which you previously installed Python and the second `venv` is the name of your `virtualenv`. You can use any other name, but stick to lowercase and use no spaces. It is also good idea to keep the name short - you'll be referencing it a lot!

### Linux and OS X

Creating a `virtualenv` on both Linux and OS X is as simple as running:

~$ python3 -m venv blog
~/djangogirls$ python3 -m venv venv

## Working with virtualenv

The command above will create a folder called `blog` that contains our virtual environment (basically bunch of folders and files). All we want to do now is starting it by running:
The command above will create a folder called `venv` that contains our virtual environment (basically bunch of folders and files). All we want to do now is starting it by running:

C:\Users\Name> blog\Scripts\activate
C:\Users\Name> venv\Scripts\activate

on Windows, or:

~$ source blog/bin/activate
~/djangogirls$ source venv/bin/activate

on OS X and Linux.

You will know that you have `virtualenv` started when you see that the prompt in your console looks like:

(blog) C:\Users\Name>
(venv) C:\Users\Name\djangogirls>

or:

(blog) ~$
(venv) ~/djangogirls$

Notice the prefix `(blog)` appears!
Notice the prefix `(venv)` appears!

When working within a virtual environment, `python` will automatically refer to the correct version so you can use `python` instead of `python3`.

Expand All @@ -60,7 +66,7 @@ OK, we have all important dependencies in place. We can finally install Django!

Now that you have your `virtualenv` started, you can install Django using `pip`. In the console, run `pip install django==1.6.5` (note that we use a double equal sign: `==`).

(blog) ~$ pip install django==1.6.5
(venv) ~$ pip install django==1.6.5
Downloading/unpacking django==1.6.5
Installing collected packages: django
Successfully installed django
Expand Down Expand Up @@ -90,7 +96,7 @@ Once that's done, enter the following command in the terminal (make sure your `v

Run the following in your console:

(blog) ~$ pip install psycopg2
(venv) ~/djangogirls$ pip install psycopg2

If that goes well, you'll see something like this

Expand Down
36 changes: 18 additions & 18 deletions django_models/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,26 @@ A model in Django is a special kind of object - it is saved in the `database` (d

### 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 the 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 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 `djangogirls` folder where `manage.py` file is) `python manage.py startapp blog`.

(blog) ~/mysite python manage.py startapp blog
(venv) ~/djangogirls$ python manage.py startapp blog

You will notice that a new `blog` folder is created and it contains a number of files now. Our folders and files in our project should look like this:

├── mysite
├── __init__.py
├── settings.py
├── urls.py
├── wsgi.py
├── manage.py
└── blog
├── __init__.py
├── admin.py
├── models.py
├── 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/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:
mysite
├── __init__.py
├── settings.py
├── urls.py
├── wsgi.py
manage.py
blog
├── __init__.py
├── admin.py
├── models.py
├── 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/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 All @@ -93,7 +93,7 @@ After creating an application we also need to tell Django that it should use it.

In a file `models.py` we define all objects called `Models` - this is a place in which we will define our blog post.

Let's open `mysite/blog/models.py`, remove everything from it and write code like this:
Let's open `blog/models.py`, remove everything from it and write code like this:

from django.db import models
from django.utils import timezone
Expand Down Expand Up @@ -144,7 +144,7 @@ If something is still not clear about models, feel free to ask your coach! We kn

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
(venv) ~/djangogirls$ python manage.py syncdb
Creating tables ...
Creating table blog_post
Installing custom SQL ...
Expand Down
Loading

0 comments on commit 68c7ec1

Please sign in to comment.