<commit_before>==================
Django at a glance
==================

Because Django was developed in a fast-paced newsroom environment, it was
designed to make common Web-development tasks fast and easy. Here's an informal
overview of how to write a database-driven Web app with Django.

The goal of this document is to give you enough technical specifics to
understand how Django works, but this isn't intended to be a tutorial or
reference. Please see our more-detailed Django documentation_ when you're ready
to start a project.

.. _documentation: ../

Design your model
=================

Although you can use Django without a database, it comes with an
object-relational mapper in which you describe your database layout in Python
code.

The data-model syntax offers many rich ways of representing your models -- so
far, it's been solving two years' worth of database-schema problems. Here's a
quick example::

    class Reporter(models.Model):
        full_name = models.CharField(max_length=70)

        def __unicode__(self):
            return self.full_name

    class Article(models.Model):
        pub_date = models.DateTimeField()
        headline = models.CharField(max_length=200)
        article = models.TextField()
        reporter = models.ForeignKey(Reporter)

        def __unicode__(self):
            return self.headline

Install it
==========

Next, run the Django command-line utility to create the database tables
automatically::

    manage.py syncdb

The ``syncdb`` command looks at all your available models and creates tables
in your database for whichever tables don't already exist.

Enjoy the free API
==================

With that, you've got a free, and rich, Python API to access your data. The API
is created on the fly, no code generation necessary::

    >>> from mysite.models import Reporter, Article

    # No reporters are in the system yet.
    >>> Reporter.objects.all()
    []

    # Create a new Reporter.
    >>> r = Reporter(full_name='<NAME>')

    # Save the object into the database. You have to call save() explicitly.
    >>> r.save()

    # Now it has an ID.
    >>> r.id
    1

    # Now the new reporter is in the database.
    >>> Reporter.objects.all()
    [<NAME>]

    # Fields are represented as attributes on the Python object.
    >>> r.full_name
    '<NAME>'

    # Django provides a rich database lookup API.
    >>> Reporter.objects.get(id=1)
    <NAME>
    >>> Reporter.objects.get(full_name__startswith='John')
    <NAME>
    >>> Reporter.objects.get(full_name__contains='mith')
    <NAME>
    >>> Reporter.objects.get(id=2)
    Traceback (most recent call last):
        ...
    DoesNotExist: Reporter does not exist for {'id__exact': 2}

    # Create an article.
    >>> from datetime import datetime
    >>> a = Article(pub_date=datetime.now(), headline='Django is cool',
    ...     article='Yeah.', reporter=r)
    >>> a.save()

    # Now the article is in the database.
    >>> Article.objects.all()
    [Django is cool]

    # Article objects get API access to related Reporter objects.
    >>> r = a.reporter
    >>> r.full_name
    '<NAME>'

    # And vice versa: Reporter objects get API access to Article objects.
    >>> r.article_set.all()
    [Django is cool]

    # The API follows relationships as far as you need, performing efficient
    # JOINs for you behind the scenes.
    # This finds all articles by a reporter whose name starts with "John".
    >>> Article.objects.filter(reporter__full_name__startswith="John")
    [Django is cool]

    # Change an object by altering its attributes and calling save().
    >>> r.full_name = '<NAME>'
    >>> r.save()

    # Delete an object with delete().
    >>> r.delete()

A dynamic admin interface: it's not just scaffolding -- it's the whole house
============================================================================

Once your models are defined, Django can automatically create a professional,
production ready administrative interface -- a Web site that lets authenticated
users add, change and delete objects. It's as easy as adding a line of code to
your model classes::

    class Article(models.Model):
        pub_date = models.DateTimeField()
        headline = models.CharField(max_length=200)
        article = models.TextField()
        reporter = models.ForeignKey(Reporter)
        class Admin: pass

The philosophy here is that your site is edited by a staff, or a client, or
maybe just you -- and you don't want to have to deal with creating backend
interfaces just to manage content.

One typical workflow in creating Django apps is to create models and get the
admin sites up and running as fast as possible, so your staff (or clients) can
start populating data. Then, develop the way data is presented to the public.

Design your URLs
================

A clean, elegant URL scheme is an important detail in a high-quality Web<commit_msg>Fixed #7999 -- Slightly changed the overview example to disambiguate the use of 'article'. Thanks to Kaell <<EMAIL>> for the suggestion.<commit_after>==================
Django at a glance
==================

Because Django was developed in a fast-paced newsroom environment, it was
designed to make common Web-development tasks fast and easy. Here's an informal
overview of how to write a database-driven Web app with Django.

The goal of this document is to give you enough technical specifics to
understand how Django works, but this isn't intended to be a tutorial or
reference. Please see our more-detailed Django documentation_ when you're ready
to start a project.

.. _documentation: ../

Design your model
=================

Although you can use Django without a database, it comes with an
object-relational mapper in which you describe your database layout in Python
code.

The data-model syntax offers many rich ways of representing your models -- so
far, it's been solving two years' worth of database-schema problems. Here's a
quick example::

    class Reporter(models.Model):
        full_name = models.CharField(max_length=70)

        def __unicode__(self):
            return self.full_name

    class Article(models.Model):
        pub_date = models.DateTimeField()
        headline = models.CharField(max_length=200)
        content = models.TextField()
        reporter = models.ForeignKey(Reporter)

        def __unicode__(self):
            return self.headline

Install it
==========

Next, run the Django command-line utility to create the database tables
automatically::

    manage.py syncdb

The ``syncdb`` command looks at all your available models and creates tables
in your database for whichever tables don't already exist.

Enjoy the free API
==================

With that, you've got a free, and rich, Python API to access your data. The API
is created on the fly, no code generation necessary::

    >>> from mysite.models import Reporter, Article

    # No reporters are in the system yet.
    >>> Reporter.objects.all()
    []

    # Create a new Reporter.
    >>> r = Reporter(full_name='<NAME>')

    # Save the object into the database. You have to call save() explicitly.
    >>> r.save()

    # Now it has an ID.
    >>> r.id
    1

    # Now the new reporter is in the database.
    >>> Reporter.objects.all()
    [John Smith]

    # Fields are represented as attributes on the Python object.
    >>> r.full_name
    '<NAME>'

    # Django provides a rich database lookup API.
    >>> Reporter.objects.get(id=1)
    <NAME>
    >>> Reporter.objects.get(full_name__startswith='John')
    <NAME>
    >>> Reporter.objects.get(full_name__contains='mith')
    <NAME>
    >>> Reporter.objects.get(id=2)
    Traceback (most recent call last):
        ...
    DoesNotExist: Reporter does not exist for {'id__exact': 2}

    # Create an article.
    >>> from datetime import datetime
    >>> a = Article(pub_date=datetime.now(), headline='Django is cool',
    ...     content='Yeah.', reporter=r)
    >>> a.save()

    # Now the article is in the database.
    >>> Article.objects.all()
    [Django is cool]

    # Article objects get API access to related Reporter objects.
    >>> r = a.reporter
    >>> r.full_name
    '<NAME>'

    # And vice versa: Reporter objects get API access to Article objects.
    >>> r.article_set.all()
    [Django is cool]

    # The API follows relationships as far as you need, performing efficient
    # JOINs for you behind the scenes.
    # This finds all articles by a reporter whose name starts with "John".
    >>> Article.objects.filter(reporter__full_name__startswith="John")
    [Django is cool]

    # Change an object by altering its attributes and calling save().
    >>> r.full_name = '<NAME>'
    >>> r.save()

    # Delete an object with delete().
    >>> r.delete()

A dynamic admin interface: it's not just scaffolding -- it's the whole house
============================================================================

Once your models are defined, Django can automatically create a professional,
production ready administrative interface -- a Web site that lets authenticated
users add, change and delete objects. It's as easy as adding a line of code to
your model classes::

    class Article(models.Model):
        pub_date = models.DateTimeField()
        headline = models.CharField(max_length=200)
        content = models.TextField()
        reporter = models.ForeignKey(Reporter)
        class Admin: pass

The philosophy here is that your site is edited by a staff, or a client, or
maybe just you -- and you don't want to have to deal with creating backend
interfaces just to manage content.

One typical workflow in creating Django apps is to create models and get the
admin sites up and running as fast as possible, so your staff (or clients) can
start populating data. Then, develop the way data is presented to the public.

Design your URLs
================

A clean, elegant URL scheme is an important detail in a high-quality Web
{'fraction_non_alphanumeric': 0.11424257674886763, 'fraction_numerical': 0.002818319073980876, 'mean_word_length': 3.3388646288209607, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 152, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '4964500', 'n_tokens_mistral': 2696, 'n_tokens_neox': 2515, 'n_words': 1163}