<commit_before>.. _intro-tutorial02:

=====================================
Writing your first Django app, part 2
=====================================

This tutorial begins where :ref:`Tutorial 1 <intro-tutorial01>` left off. We're
continuing the Web-poll application and will focus on Django's
automatically-generated admin site.

.. admonition:: Philosophy

    Generating admin sites for your staff or clients to add, change and delete
    content is tedious work that doesn't require much creativity. For that
    reason, Django entirely automates creation of admin interfaces for models.
    
    Django was written in a newsroom environment, with a very clear separation
    between "content publishers" and the "public" site. Site managers use the
    system to add news stories, events, sports scores, etc., and that content is
    displayed on the public site. Django solves the problem of creating a
    unified interface for site administrators to edit content.
    
    The admin isn't necessarily intended to be used by site visitors; it's for
    site managers.

Activate the admin site
=======================

The Django admin site is not activated by default -- it's an opt-in thing. To
activate the admin site for your installation, do these three things:

    * Add ``"django.contrib.admin"`` to your :setting:`INSTALLED_APPS` setting.
    
    * Run ``python manage.py syncdb``. Since you have added a new application
      to :setting:`INSTALLED_APPS`, the database tables need to be updated.
      
    * Edit your ``mysite/urls.py`` file and uncomment the lines below the
      "Uncomment this for admin:" comments. This file is a URLconf; we'll dig
      into URLconfs in the next tutorial. For now, all you need to know is that
      it maps URL roots to applications. In the end, you should have a
      ``urls.py`` file that looks like this:
      
      .. parsed-literal::
      
          from django.conf.urls.defaults import *

          # Uncomment the next two lines to enable the admin:
          **from django.contrib import admin**
          **admin.autodiscover()**

          urlpatterns = patterns('',
              # Example:
              # (r'^{{ project_name }}/', include('{{ project_name }}.foo.urls')),

              # Uncomment the admin/doc line below and add 'django.contrib.admindocs'
              # to INSTALLED_APPS to enable admin documentation:
              # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

              # Uncomment the next line to enable the admin:
              **(r'^admin/(.*)', admin.site.root),**
          )
          
      (The bold lines are the ones that needed to be uncommented.)

Start the development server
============================

Let's start the development server and explore the admin site.

Recall from Tutorial 1 that you start the development server like so:

.. code-block:: bash

    python manage.py runserver

Now, open a Web browser and go to "/admin/" on your local domain -- e.g.,
http://127.0.0.1:8000/admin/. You should see the admin's login screen:

.. image:: _images/admin01.png
   :alt: Django admin login screen<commit_msg>Fixed #8811: Corrected URL example in tutorial part 2<commit_after>.. _intro-tutorial02:

=====================================
Writing your first Django app, part 2
=====================================

This tutorial begins where :ref:`Tutorial 1 <intro-tutorial01>` left off. We're
continuing the Web-poll application and will focus on Django's
automatically-generated admin site.

.. admonition:: Philosophy

    Generating admin sites for your staff or clients to add, change and delete
    content is tedious work that doesn't require much creativity. For that
    reason, Django entirely automates creation of admin interfaces for models.
    
    Django was written in a newsroom environment, with a very clear separation
    between "content publishers" and the "public" site. Site managers use the
    system to add news stories, events, sports scores, etc., and that content is
    displayed on the public site. Django solves the problem of creating a
    unified interface for site administrators to edit content.
    
    The admin isn't necessarily intended to be used by site visitors; it's for
    site managers.

Activate the admin site
=======================

The Django admin site is not activated by default -- it's an opt-in thing. To
activate the admin site for your installation, do these three things:

    * Add ``"django.contrib.admin"`` to your :setting:`INSTALLED_APPS` setting.
    
    * Run ``python manage.py syncdb``. Since you have added a new application
      to :setting:`INSTALLED_APPS`, the database tables need to be updated.
      
    * Edit your ``mysite/urls.py`` file and uncomment the lines below the
      "Uncomment this for admin:" comments. This file is a URLconf; we'll dig
      into URLconfs in the next tutorial. For now, all you need to know is that
      it maps URL roots to applications. In the end, you should have a
      ``urls.py`` file that looks like this:
      
      .. parsed-literal::
      
          from django.conf.urls.defaults import *

          # Uncomment the next two lines to enable the admin:
          **from django.contrib import admin**
          **admin.autodiscover()**

          urlpatterns = patterns('',
              # Example:
              # (r'^mysite/', include('mysite.foo.urls')),

              # Uncomment the admin/doc line below and add 'django.contrib.admindocs'
              # to INSTALLED_APPS to enable admin documentation:
              # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

              # Uncomment the next line to enable the admin:
              **(r'^admin/(.*)', admin.site.root),**
          )
          
      (The bold lines are the ones that needed to be uncommented.)

Start the development server
============================

Let's start the development server and explore the admin site.

Recall from Tutorial 1 that you start the development server like so:

.. code-block:: bash

    python manage.py runserver

Now, open a Web browser and go to "/admin/" on your local domain -- e.g.,
http://127.0.0.1:8000/admin/. You should see the admin's login screen:

.. image:: _images/admin01.png
   :alt: Django admin login screen
{'fraction_non_alphanumeric': 0.11599241466498103, 'fraction_numerical': 0.00679519595448799, 'mean_word_length': 2.9482220835932624, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '890596', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1583, 'n_words': 748}