mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-16 19:41:06 +03:00
Enabled syntax highlighting in the README file.
This commit is contained in:
parent
fb3fcf0710
commit
f322e894ae
63
README.md
63
README.md
|
@ -48,48 +48,51 @@ Let's take a look at a quick example of using REST framework to build a simple m
|
||||||
|
|
||||||
Here's our project's root `urls.py` module:
|
Here's our project's root `urls.py` module:
|
||||||
|
|
||||||
from django.conf.urls.defaults import url, patterns, include
|
```python
|
||||||
from django.contrib.auth.models import User, Group
|
from django.conf.urls.defaults import url, patterns, include
|
||||||
from rest_framework import viewsets, routers
|
from django.contrib.auth.models import User, Group
|
||||||
|
from rest_framework import viewsets, routers
|
||||||
|
|
||||||
# ViewSets define the view behavior.
|
# ViewSets define the view behavior.
|
||||||
class UserViewSet(viewsets.ModelViewSet):
|
class UserViewSet(viewsets.ModelViewSet):
|
||||||
model = User
|
model = User
|
||||||
|
|
||||||
class GroupViewSet(viewsets.ModelViewSet):
|
class GroupViewSet(viewsets.ModelViewSet):
|
||||||
model = Group
|
model = Group
|
||||||
|
|
||||||
|
|
||||||
# Routers provide an easy way of automatically determining the URL conf
|
# Routers provide an easy way of automatically determining the URL conf
|
||||||
router = routers.DefaultRouter()
|
router = routers.DefaultRouter()
|
||||||
router.register(r'users', UserViewSet)
|
router.register(r'users', UserViewSet)
|
||||||
router.register(r'groups', GroupViewSet)
|
router.register(r'groups', GroupViewSet)
|
||||||
|
|
||||||
|
|
||||||
# Wire up our API using automatic URL routing.
|
# Wire up our API using automatic URL routing.
|
||||||
# Additionally, we include login URLs for the browseable API.
|
# Additionally, we include login URLs for the browseable API.
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
url(r'^', include(router.urls)),
|
url(r'^', include(router.urls)),
|
||||||
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
|
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
|
||||||
)
|
)
|
||||||
|
```
|
||||||
|
|
||||||
We'd also like to configure a couple of settings for our API.
|
We'd also like to configure a couple of settings for our API.
|
||||||
|
|
||||||
Add the following to your `settings.py` module:
|
Add the following to your `settings.py` module:
|
||||||
|
|
||||||
REST_FRAMEWORK = {
|
```python
|
||||||
# Use hyperlinked styles by default.
|
REST_FRAMEWORK = {
|
||||||
# Only used if the `serializer_class` attribute is not set on a view.
|
# Use hyperlinked styles by default.
|
||||||
'DEFAULT_MODEL_SERIALIZER_CLASS':
|
# Only used if the `serializer_class` attribute is not set on a view.
|
||||||
'rest_framework.serializers.HyperlinkedModelSerializer',
|
'DEFAULT_MODEL_SERIALIZER_CLASS':
|
||||||
|
'rest_framework.serializers.HyperlinkedModelSerializer',
|
||||||
# Use Django's standard `django.contrib.auth` permissions,
|
|
||||||
# or allow read-only access for unauthenticated users.
|
|
||||||
'DEFAULT_PERMISSION_CLASSES': [
|
|
||||||
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
# Use Django's standard `django.contrib.auth` permissions,
|
||||||
|
# or allow read-only access for unauthenticated users.
|
||||||
|
'DEFAULT_PERMISSION_CLASSES': [
|
||||||
|
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
Don't forget to make sure you've also added `rest_framework` to your `INSTALLED_APPS` setting.
|
Don't forget to make sure you've also added `rest_framework` to your `INSTALLED_APPS` setting.
|
||||||
|
|
||||||
That's it, we're done!
|
That's it, we're done!
|
||||||
|
|
Loading…
Reference in New Issue
Block a user