Differences between RESTful API and urlpatterns router in Django

情到浓时终转凉″ 提交于 2021-01-27 06:58:24

问题


Am new to web developpement and wondering the differences between:

Django Restful API

and

standard Django URL routers

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.index, name='Index'),
    url(r'^getvalue/$', views.get_points, name='Get Points'),
    url(r'^putvalue/$', views.put_points, name='Put Points'),
]

What are the benefits of setting Django restful API when interacting with Javascript components since both are JSON sending URL ?


回答1:


Before understanding this you have know that,

  • REST API concept.
  • HTTP verbs(REQUEST METHOD)

REST API

REST API is nothing but very special. Just remember one thing this is a concept where we can use proper use of HTTP VERBS. Like, GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS etc....

HTTP VERBS

I already told you the names of HTTP VERBS. Think what we do normally?? Basically I do, I use POST for updating db row, I use POST for DELETE a row. But in REST API concept we can't do like this kinds of nasty things. When we are going to delete something we need to use DELETE

Links

You may read this, https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html




回答2:


It was quite some time since the question had been asked, but I thought I'd provide an answer as it bothered me as well.

The main reason why you want to use Django's routers is convenience: once you declared them once, you don't have to go through the same cumbersomeness as persistent declaration of urls in urls_patterns.

However, the url_patterns can still be used if for any reason you want to have a specific hard-coded url.



来源:https://stackoverflow.com/questions/42184551/differences-between-restful-api-and-urlpatterns-router-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!