mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-08 14:24:48 +03:00
Update 3-class-based-views.md
This commit is contained in:
parent
3a1147ccac
commit
2b56d91af2
|
@ -1,10 +1,10 @@
|
|||
# Tutorial 3: Class Based Views
|
||||
# Tutorial 3: Class-based Views
|
||||
|
||||
We can also write our API views using class based views, rather than function based views. As we'll see this is a powerful pattern that allows us to reuse common functionality, and helps us keep our code [DRY][dry].
|
||||
We can also write our API views using class-based views, rather than function based views. As we'll see this is a powerful pattern that allows us to reuse common functionality, and helps us keep our code [DRY][dry].
|
||||
|
||||
## Rewriting our API using class based views
|
||||
## Rewriting our API using class-based views
|
||||
|
||||
We'll start by rewriting the root view as a class based view. All this involves is a little bit of refactoring of `views.py`.
|
||||
We'll start by rewriting the root view as a class-based view. All this involves is a little bit of refactoring of `views.py`.
|
||||
|
||||
from snippets.models import Snippet
|
||||
from snippets.serializers import SnippetSerializer
|
||||
|
@ -62,7 +62,7 @@ So far, so good. It looks pretty similar to the previous case, but we've got be
|
|||
|
||||
That's looking good. Again, it's still pretty similar to the function based view right now.
|
||||
|
||||
We'll also need to refactor our `urls.py` slightly now we're using class based views.
|
||||
We'll also need to refactor our `urls.py` slightly now we're using class-based views.
|
||||
|
||||
from django.conf.urls import url
|
||||
from rest_framework.urlpatterns import format_suffix_patterns
|
||||
|
@ -79,7 +79,7 @@ Okay, we're done. If you run the development server everything should be workin
|
|||
|
||||
## Using mixins
|
||||
|
||||
One of the big wins of using class based views is that it allows us to easily compose reusable bits of behaviour.
|
||||
One of the big wins of using class-based views is that it allows us to easily compose reusable bits of behaviour.
|
||||
|
||||
The create/retrieve/update/delete operations that we've been using so far are going to be pretty similar for any model-backed API views we create. Those bits of common behaviour are implemented in REST framework's mixin classes.
|
||||
|
||||
|
@ -124,7 +124,7 @@ The base class provides the core functionality, and the mixin classes provide th
|
|||
|
||||
Pretty similar. Again we're using the `GenericAPIView` class to provide the core functionality, and adding in mixins to provide the `.retrieve()`, `.update()` and `.destroy()` actions.
|
||||
|
||||
## Using generic class based views
|
||||
## Using generic class-based views
|
||||
|
||||
Using the mixin classes we've rewritten the views to use slightly less code than before, but we can go one step further. REST framework provides a set of already mixed-in generic views that we can use to trim down our `views.py` module even more.
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user