|
| 1 | +# Starting Django project |
| 2 | + |
| 3 | +*This chapter is based on awesome tutorials by Geek Girls Carrots (http://django.carrots.pl/) and django-marcador (http://django-marcador.keimlink.de/).* |
| 4 | + |
| 5 | +Let's build a simple blog! |
| 6 | + |
| 7 | +The first step to create it is starting a new Django project. Basically that means that we will run some script from Django that will create for us a skeleton of Django project: bunch of directories and files that we will later use. |
| 8 | + |
| 9 | +Names of some files and directories are very important for Django. You should not rename files that we are about to create. Moving them to different place is also not a good idea. Django needs a certain folder/files structure to be able to find important things. |
| 10 | + |
| 11 | +In console/command-line you should type: |
| 12 | + |
| 13 | + (blog) ~$ django-admin.py startproject mysite |
| 14 | + |
| 15 | +`django-admin.py` is a script that will create folders and files for you. It should look like this: |
| 16 | + |
| 17 | + mysite |
| 18 | + │ manage.py |
| 19 | + │ |
| 20 | + └───mysite |
| 21 | + settings.py |
| 22 | + urls.py |
| 23 | + wsgi.py |
| 24 | + __init__.py |
| 25 | + |
| 26 | + |
| 27 | +`manage.py` is a script that helps with administration of the page. Thanks to it we will be able to start a web server on our computer without installing anything else. |
| 28 | + |
| 29 | +File `settings.py` contains a configuration of your website. |
| 30 | + |
| 31 | +Remember when we talked about a postman checking where to deliver a letter? `urls.py` file contains a list of patterns used by `urlresolver`. |
| 32 | + |
| 33 | +Let's ignore for now other files - we will not change them, so we will skip expalining what they actually do. The only thing to remember is to not delete them by accident! |
| 34 | + |
| 35 | +## Changing settings |
| 36 | + |
| 37 | +Let's do some changes in `settings.py`. |
| 38 | + |
| 39 | +It would be nice to have a correct time on our website. You should find lines that contain `USE_TZ` and `TIME_ZONE` and change them to look like this: |
| 40 | + |
| 41 | + USE_TZ = False |
| 42 | + TIME_ZONE = 'Europe/Berlin' |
| 43 | + |
| 44 | +## Creating a database |
| 45 | + |
| 46 | +Most of web application have their own database. Database is a collection of data. This is a place in which you will store all information about users, all your blog post texts, etc.. |
| 47 | + |
| 48 | +There is number of different database systems that store data for you. We will use one of the simpliest: `sqlite3`, which was installed with Django. But if you plan to do some serious websites in a future we recommends looking at `PostgreSQL`. |
| 49 | + |
| 50 | +To create a database for our blog we will type in console/command-line `python manage.py syncdb`. It should look like this: |
| 51 | + |
| 52 | + (blog) ~/carrots$ python manage.py syncdb |
| 53 | + Creating tables ... |
| 54 | + Creating table auth_permission |
| 55 | + Creating table auth_group_permissions |
| 56 | + Creating table auth_group |
| 57 | + Creating table auth_user_groups |
| 58 | + Creating table auth_user_user_permissions |
| 59 | + Creating table auth_user |
| 60 | + Creating table django_content_type |
| 61 | + Creating table django_session |
| 62 | + Creating table django_site |
| 63 | + Creating table django_admin_log |
| 64 | + |
| 65 | + You just installed Django's auth system, which means you don't have any superusers defined. |
| 66 | + Would you like to create one now? (yes/no): yes |
| 67 | + Username (leave blank to use 'Name'): |
| 68 | + Email address: admin@example.com |
| 69 | + Password: |
| 70 | + Password (again): |
| 71 | + Superuser created successfully. |
| 72 | + Installing custom SQL ... |
| 73 | + Installing indexes ... |
| 74 | + Installed 0 object(s) from 0 fixture(s) |
| 75 | + |
| 76 | +It will ask you if you want to create a superuser. Type `yes`, press Enter and type your username (lowercase, no spaces), email address and password when you will be asked to type it. |
| 77 | + |
| 78 | +And you are done. Time to start web server and see if our website is working! |
| 79 | + |
| 80 | +You need to be in a folder that contains `manage.py` file (in `mysite` directory). In console/command-line we will start the web server by typing `python manage.py runserver`: |
| 81 | + |
| 82 | + (blog) ~/mysite python manage.py runserver |
| 83 | + |
| 84 | + |
0 commit comments