Dev Authentication:
In the previous part me met a status 403.
We can adjust the setting in rest framework to allow creating new tweet in reactjs.
- add DEFAULT_AUTHENTICATION_CLASSES in /Twittme/settings.py
```python
…
DEFAULT_AUTHENTICATION_CLASSES = [ # new ‘rest_framework.authentication.SessionAuthentication’ ]
if DEBUG: DEFAULT_RENDERER_CLASSES += [ ‘rest_framework.renderers.BrowsableAPIRenderer’, ] DEFAULT_AUTHENTICATION_CLASSES += [ # new
]
REST_FRAMEWORK = {
# 'DEFAULT_AUTHENTICATION_CLASSES': ['rest_framework.authentication.SessionAuthentication']'DEFAULT_AUTHENTICATION_CLASSES': DEFAULT_AUTHENTICATION_CLASSES,'DEFAULT_RENDERER_CLASSES': DEFAULT_RENDERER_CLASSES
}
- Under Twittme/ directory, create a new directory rest_api/<br />Also, create 2 new files __init__.py and dev.py in rest_api/<br />(__init__.py must be **double underscore**)- Then in /Twittme/settings.py```pythonif DEBUG:DEFAULT_RENDERER_CLASSES += ['rest_framework.renderers.BrowsableAPIRenderer',]DEFAULT_AUTHENTICATION_CLASSES += ['Twittme.rest_api.dev.DevAuthentication' # new]
This is to import authentication from dev.py we just created.
- add contents of authentication in Twittme/rest_api/dev.py ```python from django.contrib.auth import get_user_model from rest_framework import authentication
User = get_user_model()
class DevAuthentication(authentication.BasicAuthentication): def authenticate(self, request): qs = User.objects.all() user = qs.order_by(“?”).first() return (user, None) ```
