首页 > 代码库 > <Effective Django>读书笔记

<Effective Django>读书笔记

  1. In Django parlance, a project is the final product, and it assembles one or more applications together.
  2. 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
  3. Settings.py is where you’ll configure your project. It has a few sensible defaults, but no database chosen when you start.
  4. Urls.py contains the URL to view mappings.
  5. Wsgi.py is a WSGI wrapper for your application.
  6. Beginning in Django 1.4, apps are placed alongside project packages.
  7. Models.py will contain the Django ORM models for your app.
  8. Views.py will contain the View code
  9. Tests.py will contain the unit and integration tests you write
  10. Django models map to a database table, and provide a place to encapsulate business logic. All models subclass the base Model
  11. 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.
  12. By default Django will name your tables using a combination of the application name and model name.
  13. Django adds an id field as the primary key for your model, unless you specify a primary key.
  14. The syncdb manage command creates the tables in your database from models.
  15. The model Manager allows you to operate on the collection of instanes: querying, creating, etc
  16. Write unit tests for methods you add to the model
  17. The test manage command runs the unit tests.
  18. Django Views take an HTTP Request and return an HTTP Response to the user.
  19. 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.
  20. Generic Views have always provided some basic functionality: render a template, redirect, create or edit a model.
  21. 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.
  22. By default Django will look for templates in applications, as well as in directories you specify in settings.TEMPLATE_DIRS.
  23. 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.
  24. 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
  25. Django has two tools that are helpful for writing unit tests for views: the Test Client and the RequestFactory
  26. 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.
  27. 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.
  28. 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.
  29. Django distinguishes between static site files, and user upload media.
  30. The staticfiles app is included to help manage static files and serve them during development.
  31. Static files can be included with apps, or with the project.
  32. Templates can extend one another, using block tags.
  33. A Django Form is responsible for taking some user input, validating it, and turning it into Python objects.
  34. Initial is a dictionary of values that will be used as the default values for an unbound form
  35. 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.
  36. Form validation takes place in a form’s clean() method
  37. Forms take user input, validate it, and convert it to Python objects.
  38. 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.
  39. A FormSet is an object that manages multiple copies of the same Form(or ModelForm) in a single page.