Fixed #14426 -- Removed "mysite" import statements from examples that might teach people "bad habits" in regards to creating reusable apps.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14270 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
2790cf482d
commit
7baee5b953
|
@ -21,7 +21,8 @@ code.
|
||||||
|
|
||||||
The :doc:`data-model syntax </topics/db/models>` offers many rich ways of
|
The :doc:`data-model syntax </topics/db/models>` offers many rich ways of
|
||||||
representing your models -- so far, it's been solving two years' worth of
|
representing your models -- so far, it's been solving two years' worth of
|
||||||
database-schema problems. Here's a quick example::
|
database-schema problems. Here's a quick example, which might be saved in
|
||||||
|
the file ``mysite/news/models.py``::
|
||||||
|
|
||||||
class Reporter(models.Model):
|
class Reporter(models.Model):
|
||||||
full_name = models.CharField(max_length=70)
|
full_name = models.CharField(max_length=70)
|
||||||
|
@ -57,7 +58,8 @@ Enjoy the free API
|
||||||
With that, you've got a free, and rich, :doc:`Python API </topics/db/queries>` to
|
With that, you've got a free, and rich, :doc:`Python API </topics/db/queries>` to
|
||||||
access your data. The API is created on the fly, no code generation necessary::
|
access your data. The API is created on the fly, no code generation necessary::
|
||||||
|
|
||||||
>>> from mysite.models import Reporter, Article
|
# Import the models we created from our "news" app
|
||||||
|
>>> from news.models import Reporter, Article
|
||||||
|
|
||||||
# No reporters are in the system yet.
|
# No reporters are in the system yet.
|
||||||
>>> Reporter.objects.all()
|
>>> Reporter.objects.all()
|
||||||
|
@ -177,9 +179,9 @@ example above::
|
||||||
from django.conf.urls.defaults import *
|
from django.conf.urls.defaults import *
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
(r'^articles/(\d{4})/$', 'mysite.views.year_archive'),
|
(r'^articles/(\d{4})/$', 'news.views.year_archive'),
|
||||||
(r'^articles/(\d{4})/(\d{2})/$', 'mysite.views.month_archive'),
|
(r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'),
|
||||||
(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'mysite.views.article_detail'),
|
(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'),
|
||||||
)
|
)
|
||||||
|
|
||||||
The code above maps URLs, as simple regular expressions, to the location of
|
The code above maps URLs, as simple regular expressions, to the location of
|
||||||
|
@ -195,7 +197,7 @@ is a simple Python function. Each view gets passed a request object --
|
||||||
which contains request metadata -- and the values captured in the regex.
|
which contains request metadata -- and the values captured in the regex.
|
||||||
|
|
||||||
For example, if a user requested the URL "/articles/2005/05/39323/", Django
|
For example, if a user requested the URL "/articles/2005/05/39323/", Django
|
||||||
would call the function ``mysite.views.article_detail(request,
|
would call the function ``news.views.article_detail(request,
|
||||||
'2005', '05', '39323')``.
|
'2005', '05', '39323')``.
|
||||||
|
|
||||||
Write your views
|
Write your views
|
||||||
|
|
|
@ -193,7 +193,7 @@ wizard takes a list of your :class:`~django.forms.Form` objects as
|
||||||
arguments when you instantiate the Wizard::
|
arguments when you instantiate the Wizard::
|
||||||
|
|
||||||
from django.conf.urls.defaults import *
|
from django.conf.urls.defaults import *
|
||||||
from mysite.testapp.forms import ContactForm1, ContactForm2, ContactWizard
|
from testapp.forms import ContactForm1, ContactForm2, ContactWizard
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
(r'^contact/$', ContactWizard([ContactForm1, ContactForm2])),
|
(r'^contact/$', ContactWizard([ContactForm1, ContactForm2])),
|
||||||
|
|
|
@ -95,7 +95,7 @@ sitemap to include all the links to your individual blog entries. Here's how
|
||||||
your sitemap class might look::
|
your sitemap class might look::
|
||||||
|
|
||||||
from django.contrib.sitemaps import Sitemap
|
from django.contrib.sitemaps import Sitemap
|
||||||
from mysite.blog.models import Entry
|
from blog.models import Entry
|
||||||
|
|
||||||
class BlogSitemap(Sitemap):
|
class BlogSitemap(Sitemap):
|
||||||
changefreq = "never"
|
changefreq = "never"
|
||||||
|
@ -242,7 +242,7 @@ Here's an example of a :doc:`URLconf </topics/http/urls>` using both::
|
||||||
|
|
||||||
from django.conf.urls.defaults import *
|
from django.conf.urls.defaults import *
|
||||||
from django.contrib.sitemaps import FlatPageSitemap, GenericSitemap
|
from django.contrib.sitemaps import FlatPageSitemap, GenericSitemap
|
||||||
from mysite.blog.models import Entry
|
from blog.models import Entry
|
||||||
|
|
||||||
info_dict = {
|
info_dict = {
|
||||||
'queryset': Entry.objects.all(),
|
'queryset': Entry.objects.all(),
|
||||||
|
|
|
@ -570,7 +570,7 @@ It's perfectly OK to relate a model to one from another app. To do this,
|
||||||
import the related model at the top of the model that holds your model. Then,
|
import the related model at the top of the model that holds your model. Then,
|
||||||
just refer to the other model class wherever needed. For example::
|
just refer to the other model class wherever needed. For example::
|
||||||
|
|
||||||
from mysite.geography.models import ZipCode
|
from geography.models import ZipCode
|
||||||
|
|
||||||
class Restaurant(models.Model):
|
class Restaurant(models.Model):
|
||||||
# ...
|
# ...
|
||||||
|
|
|
@ -60,7 +60,7 @@ funky model importing.)
|
||||||
|
|
||||||
Assuming models live in a file ``mysite/blog/models.py``, here's an example::
|
Assuming models live in a file ``mysite/blog/models.py``, here's an example::
|
||||||
|
|
||||||
>>> from mysite.blog.models import Blog
|
>>> from blog.models import Blog
|
||||||
>>> b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.')
|
>>> b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.')
|
||||||
>>> b.save()
|
>>> b.save()
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ Updating a ``ForeignKey`` field works exactly the same way as saving a normal
|
||||||
field; simply assign an object of the right type to the field in question.
|
field; simply assign an object of the right type to the field in question.
|
||||||
This example updates the ``blog`` attribute of an ``Entry`` instance ``entry``::
|
This example updates the ``blog`` attribute of an ``Entry`` instance ``entry``::
|
||||||
|
|
||||||
>>> from mysite.blog.models import Entry
|
>>> from blog.models import Entry
|
||||||
>>> entry = Entry.objects.get(pk=1)
|
>>> entry = Entry.objects.get(pk=1)
|
||||||
>>> cheese_blog = Blog.objects.get(name="Cheddar Talk")
|
>>> cheese_blog = Blog.objects.get(name="Cheddar Talk")
|
||||||
>>> entry.blog = cheese_blog
|
>>> entry.blog = cheese_blog
|
||||||
|
@ -108,7 +108,7 @@ Updating a ``ManyToManyField`` works a little differently; use the ``add()``
|
||||||
method on the field to add a record to the relation. This example adds the
|
method on the field to add a record to the relation. This example adds the
|
||||||
``Author`` instance ``joe`` to the ``entry`` object::
|
``Author`` instance ``joe`` to the ``entry`` object::
|
||||||
|
|
||||||
>>> from mysite.blog.models import Author
|
>>> from blog.models import Author
|
||||||
>>> joe = Author.objects.create(name="Joe")
|
>>> joe = Author.objects.create(name="Joe")
|
||||||
>>> entry.authors.add(joe)
|
>>> entry.authors.add(joe)
|
||||||
|
|
||||||
|
|
|
@ -72,7 +72,7 @@ the URLconf to point to a view function:
|
||||||
|
|
||||||
from django.conf.urls.defaults import *
|
from django.conf.urls.defaults import *
|
||||||
from django.views.generic.simple import direct_to_template
|
from django.views.generic.simple import direct_to_template
|
||||||
**from mysite.books.views import about_pages**
|
**from books.views import about_pages**
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
('^about/$', direct_to_template, {
|
('^about/$', direct_to_template, {
|
||||||
|
@ -152,7 +152,7 @@ To build a list page of all publishers, we'd use a URLconf along these lines::
|
||||||
|
|
||||||
from django.conf.urls.defaults import *
|
from django.conf.urls.defaults import *
|
||||||
from django.views.generic import list_detail
|
from django.views.generic import list_detail
|
||||||
from mysite.books.models import Publisher
|
from books.models import Publisher
|
||||||
|
|
||||||
publisher_info = {
|
publisher_info = {
|
||||||
"queryset" : Publisher.objects.all(),
|
"queryset" : Publisher.objects.all(),
|
||||||
|
@ -251,7 +251,7 @@ detail view, we'd use an info dict like this:
|
||||||
|
|
||||||
.. parsed-literal::
|
.. parsed-literal::
|
||||||
|
|
||||||
from mysite.books.models import Publisher, **Book**
|
from books.models import Publisher, **Book**
|
||||||
|
|
||||||
publisher_info = {
|
publisher_info = {
|
||||||
"queryset" : Publisher.objects.all(),
|
"queryset" : Publisher.objects.all(),
|
||||||
|
@ -376,7 +376,7 @@ of code by hand. As usual, we'll start by writing a URLconf:
|
||||||
|
|
||||||
.. parsed-literal::
|
.. parsed-literal::
|
||||||
|
|
||||||
from mysite.books.views import books_by_publisher
|
from books.views import books_by_publisher
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
(r'^publishers/$', list_detail.object_list, publisher_info),
|
(r'^publishers/$', list_detail.object_list, publisher_info),
|
||||||
|
@ -387,7 +387,7 @@ Next, we'll write the ``books_by_publisher`` view itself::
|
||||||
|
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
from django.views.generic import list_detail
|
from django.views.generic import list_detail
|
||||||
from mysite.books.models import Book, Publisher
|
from books.models import Book, Publisher
|
||||||
|
|
||||||
def books_by_publisher(request, name):
|
def books_by_publisher(request, name):
|
||||||
|
|
||||||
|
@ -447,7 +447,7 @@ custom view:
|
||||||
|
|
||||||
.. parsed-literal::
|
.. parsed-literal::
|
||||||
|
|
||||||
from mysite.books.views import author_detail
|
from books.views import author_detail
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
#...
|
#...
|
||||||
|
@ -457,7 +457,7 @@ custom view:
|
||||||
Then we'd write our wrapper function::
|
Then we'd write our wrapper function::
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
from mysite.books.models import Author
|
from books.models import Author
|
||||||
from django.views.generic import list_detail
|
from django.views.generic import list_detail
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
|
|
||||||
|
|
|
@ -338,12 +338,12 @@ Here's the example URLconf from the :doc:`Django overview </intro/overview>`::
|
||||||
from django.conf.urls.defaults import *
|
from django.conf.urls.defaults import *
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
(r'^articles/(\d{4})/$', 'mysite.news.views.year_archive'),
|
(r'^articles/(\d{4})/$', 'news.views.year_archive'),
|
||||||
(r'^articles/(\d{4})/(\d{2})/$', 'mysite.news.views.month_archive'),
|
(r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'),
|
||||||
(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'mysite.news.views.article_detail'),
|
(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'),
|
||||||
)
|
)
|
||||||
|
|
||||||
In this example, each view has a common prefix -- ``'mysite.news.views'``.
|
In this example, each view has a common prefix -- ``'news.views'``.
|
||||||
Instead of typing that out for each entry in ``urlpatterns``, you can use the
|
Instead of typing that out for each entry in ``urlpatterns``, you can use the
|
||||||
first argument to the ``patterns()`` function to specify a prefix to apply to
|
first argument to the ``patterns()`` function to specify a prefix to apply to
|
||||||
each view function.
|
each view function.
|
||||||
|
@ -352,7 +352,7 @@ With this in mind, the above example can be written more concisely as::
|
||||||
|
|
||||||
from django.conf.urls.defaults import *
|
from django.conf.urls.defaults import *
|
||||||
|
|
||||||
urlpatterns = patterns('mysite.news.views',
|
urlpatterns = patterns('news.views',
|
||||||
(r'^articles/(\d{4})/$', 'year_archive'),
|
(r'^articles/(\d{4})/$', 'year_archive'),
|
||||||
(r'^articles/(\d{4})/(\d{2})/$', 'month_archive'),
|
(r'^articles/(\d{4})/(\d{2})/$', 'month_archive'),
|
||||||
(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'article_detail'),
|
(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'article_detail'),
|
||||||
|
|
Loading…
Reference in New Issue