******************** A new Django Project ******************** .. index:: Project structure, startproject Start the Django Project ======================== You can create a new Django project with the following command: :: $ django-admin.py startproject mysite After you've run the command you'll find the following structure: :: mysite |-- manage.py `-- mysite |-- __init__.py |-- settings.py |-- urls.py `-- wsgi.py .. index:: Development server, runserver Test the Development Server =========================== After you've created the project, you can change to the directory :file:`mysite`: :: $ cd mysite And try out the development server with the following command: .. literalinclude:: runserver.log .. note:: If you are on Windows and the command fails with an ``UnicodeDecodeError``, use this command instead: :: $ python manage.py runserver 0.0.0.0:8000 .. raw:: latex \newpage Now you can open the "Welcome to Django" site from http://127.0.0.1:8000/. After you've opened the site, you can kill the development server with :kbd:`CTRL + C`. .. image:: /images/welcome-to-django.* :alt: Welcome to Django :align: center .. index:: Configuration Configuration ============= In order to work with the project, you need to configure it. To do that, open the file :file:`settings.py` in a text editor. So that you don't need to enter the project directory several times in the configuration, it's saved in a "constant". This constant can then be used everywhere where the project directory is required. You can find it right at the top of the :file:`settings.py` file: .. literalinclude:: ../src/mysite/mysite/settings.py :lines: 13-16 :lineno-start: 13 .. doctest:: :hide: >>> settings.BASE_DIR.endswith('/mysite') True .. raw:: latex \newpage The first thing that needs to be configured is the path where the templates will be located. The :file:`settings.py` already contains a ``TEMPLATES`` constant which is preconfigured to use Django's template engine. Add the path to the templates directory to the ``DIRS`` list: .. literalinclude:: ../src/mysite/mysite/settings.py :lines: 57-71 :emphasize-lines: 4 :lineno-start: 57 .. doctest:: :hide: >>> settings.TEMPLATES[0]['DIRS'][0].startswith(settings.BASE_DIR) True >>> settings.TEMPLATES[0]['DIRS'][0].endswith('templates') True The the existing database connection ``default`` is already configured to use `SQLite `_, because it's built into Python: .. literalinclude:: ../src/mysite/mysite/settings.py :lines: 76-84 :emphasize-lines: 5-8 :lineno-start: 76 .. doctest:: :hide: >>> settings.DATABASES['default']['ENGINE'].endswith('sqlite3') True >>> settings.DATABASES['default']['NAME'].startswith(settings.BASE_DIR) True Next change the timezone and language to suit: .. literalinclude:: ../src/mysite/mysite/settings.py :lines: 87-92 :lineno-start: 87 .. doctest:: :hide: >>> settings.TIME_ZONE == 'Europe/Berlin' True >>> settings.LANGUAGE_CODE == 'en-us' True The constant ``LANGUAGE_CODE`` configures the language of the Admin inferface which we will use later to English. You can change it to a different language, e.g. use ``de`` as ``LANGUAGE_CODE`` if you want to use German. Lastly, the path to static files must be defined at the end of :file:`settings.py` by adding the ``STATICFILES_DIRS`` setting: .. literalinclude:: ../src/mysite/mysite/settings.py :lines: 101-108 :emphasize-lines: 6-8 :lineno-start: 106 .. doctest:: :hide: >>> settings.STATICFILES_DIRS[0].startswith(settings.BASE_DIR) True >>> settings.STATICFILES_DIRS[0].endswith('static') True Now create the directory for static files and templates under directory :file:`mysite`:: $ mkdir static templates .. raw:: latex \newpage Afterwards the directory structure should look as follows: :: mysite |-- manage.py |-- mysite | |-- __init__.py | |-- settings.py | |-- urls.py | `-- wsgi.py |-- static `-- templates