- In Django parlance, a project is the final product, and it assembles one
or more applications together. - Manage.py is a pointer back to Django-admin.py with an environment
variable set, pointing to your project as the one to read settings from and
operate on when needed - Settings.py is where you’ll configure your project. It has a few sensible
defaults, but no database chosen when you start. - Urls.py contains the URL to view mappings.
- Wsgi.py is a WSGI wrapper for your application.
- Beginning in Django 1.4, apps are placed alongside project packages.
- Models.py will contain the Django ORM models for your app.
- Views.py will contain the View code
- Tests.py will contain the unit and integration tests you write
- Django models map to a database table, and provide a place to encapsulate
business logic. All models subclass the base Model - The INSTALLED_APPS setting lists the applications that the project uses.
These are listed as strings that map to Python packages. Django will import
each and looks for a models there. - By default Django will name your tables using a combination of the
application name and model name. - Django adds an id field as the primary key for your model, unless you
specify a primary key. - The syncdb manage command creates the tables in your database from
models. - The model Manager allows you to operate on the collection of instanes:
querying, creating, etc - Write unit tests for methods you add to the model
- The test manage command runs the unit tests.
- Django Views take an HTTP Request and return an HTTP Response to the
user. - Any Python callable can be a view. The only hard and fast requirement is
that it takes the request object (customarily named request) as its first
argument. - Generic Views have always provided some basic functionality: render a
template, redirect, create or edit a model. - The URL configuration tells Django how to match a request’s path to your
Python code. Django looks for the URL configuration, defined as urlpatterns,
in the urls.py file in your project. - By default Django will look for templates in applications, as well as in
directories you specify in settings.TEMPLATE_DIRS. - Most generic views that do form processing have the concept of the
“success URL”: where to redirect the user when the form is successfully
submitted. - The collection of values available to a template when it’s rendered is
referred to as the Context. The Context is a combination of information
supplied by the view and information from context processors - Django has two tools that are helpful for writing unit tests for views:
the Test Client and the RequestFactory - The TestClient takes a URL to retrieve, and resolves it against your
project’s URL configuration. It then creates a test request, and passes that
request through your view, returning the Response. - Django supports adding static files at both the application and project
level. Where you add them sort of depends on how tied to your specific
assembly of apps they are. - App specific static files are stored in the static subdirectory within the
app. Django will also look in any directories listed in the STATIFFILES_DIRS
setting. - Django distinguishes between static site files, and user upload
media. - The staticfiles app is included to help manage static files and serve them
during development. - Static files can be included with apps, or with the project.
- Templates can extend one another, using block tags.
- A Django Form is responsible for taking some user input, validating it,
and turning it into Python objects. - Initial is a dictionary of values that will be used as the default values
for an unbound form - Forms have two different phases of validation: field and form. All the
fields are validated and converted to Python objects (if possible) before form
validation begins. - Form validation takes place in a form’s clean() method
- Forms take user input, validate it, and convert it to Python objects.
- Django provides 3 types of fields for relating objects to each other:
ForeignKey for creating one to many relationships, ManyToManyField for
relating many to many, and OneToOneField for creating a one to one
relationship. - A FormSet is an object that manages multiple copies of the same Form(or
ModelForm) in a single page.
<Effective Django>读书笔记
时间: 2024-10-03 07:54:39