Merge pull request #1094 from caitinggui/master

update class_based_views
This commit is contained in:
Raphael Deem 2018-01-23 17:25:59 -08:00 committed by GitHub
commit 72d56a89a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -92,10 +92,27 @@ class ViewWithDecorator(HTTPMethodView):
def get(self, request, name): def get(self, request, name):
return text('Hello I have a decorator') return text('Hello I have a decorator')
def post(self, request, name):
return text("Hello I also have a decorator")
app.add_route(ViewWithDecorator.as_view(), '/url') app.add_route(ViewWithDecorator.as_view(), '/url')
``` ```
#### URL Building But if you just want to decorate some functions and not all functions, you can do as follows:
```python
class ViewWithSomeDecorator(HTTPMethodView):
@staticmethod
@some_decorator_here
def get(request, name):
return text("Hello I have a decorator")
def post(self, request, name):
return text("Hello I don't have any decorators")
```
## URL Building
If you wish to build a URL for an HTTPMethodView, remember that the class name will be the endpoint If you wish to build a URL for an HTTPMethodView, remember that the class name will be the endpoint
that you will pass into `url_for`. For example: that you will pass into `url_for`. For example: