Want to learn django with REST without rest framew

2019-08-29 10:39发布

问题:

I am a php programmer, I have built some REST based solutions in php. Now I am learning python/django. I want to make a REST based solution in Django ( only for knowledge purpose ). I do not want to use any of REST frameworks/toolkits as This project is more a exploring django/python say how they work with raw REST concept.

I searched on net, But examples/tutorial filled on already built solutions. I also checkout for request method based filtering. I am thinking of two approaches.

Either urls.py have way to check request method and transfer to respective method in views.py.
Or I can add a pre load hook/class which determine request method on application initialize, And called respective method so overriding urls.py behavior (my preferred method).

If anybody can suggest a django way to do this?

Update : I found some interesting comments on SO, like https://stackoverflow.com/a/20898410/1230744 AND https://stackoverflow.com/a/1732520/1230744. Need to check if they can have the solution, I am searching.

回答1:

You can start from learning the code of this projects:

  • http://tastypieapi.org/ Tastypie
  • http://www.django-rest-framework.org/ Django REST framework

They are snadrd de facto for REST API for Django and their code could be a good starting point.

Also, please review this questions:

  • Creating a REST API for a Django application
  • Adding REST to Django
  • Django and Restful APIs


回答2:

Well I get the answer of my questions finally from following link. It is possible through using Class based Views + serialization.

Restful routes and Django

Snippet links in side above link gave pretty much example to gave quite picture of how one can create a REST Api using only Django Core. Also I used serialize https://docs.djangoproject.com/en/dev/topics/serialization/ for Json encoding

( Now if anybody prefer, he can flag duplicate the question. ;) )