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

  1. ]

REST_FRAMEWORK = {

  1. # 'DEFAULT_AUTHENTICATION_CLASSES': ['rest_framework.authentication.SessionAuthentication']
  2. 'DEFAULT_AUTHENTICATION_CLASSES': DEFAULT_AUTHENTICATION_CLASSES,
  3. 'DEFAULT_RENDERER_CLASSES': DEFAULT_RENDERER_CLASSES

}

  1. - ![image.png](https://cdn.nlark.com/yuque/0/2020/png/1243266/1593907213980-19dda272-c5d1-4e3d-9634-1c125e3aa2ee.png#align=left&display=inline&height=163&margin=%5Bobject%20Object%5D&name=image.png&originHeight=325&originWidth=186&size=11269&status=done&style=none&width=93)
  2. 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**)
  3. - Then in /Twittme/settings.py
  4. ```python
  5. if DEBUG:
  6. DEFAULT_RENDERER_CLASSES += [
  7. 'rest_framework.renderers.BrowsableAPIRenderer',
  8. ]
  9. DEFAULT_AUTHENTICATION_CLASSES += [
  10. 'Twittme.rest_api.dev.DevAuthentication' # new
  11. ]

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) ```