0

This is a very general and open-topic question. So yesterday we decided we would like to create an iOS app based off our website that is currently being built with Django. Now, Django, like RoR, is suppose to be transparent to the UI; it shouldn't care what UI the user is using. (Proper MVC).

If I look at some of our code for example, here is how we add an equipment into our system:

 @login_required
 def add(request):
     r_user = request.user.userprofile
     form = EquipmentFormAdd(request.POST or None, c_id=r_user.company_id, error_class=DivErrorList)
     if form.is_valid():
        equipment = form.save(commit=False)
        equipment.company_id = r_user.company_id
        equipment.added_by_id = request.user.id
        default_file_path = EquipmentPicture.get_default_file_path()
        url_bucket = r_user.company.s3_bucket.name + default_file_path        
        cell = form.cleaned_data['cell']
        equipment.cell_order = cell.equipment_set.count() + 1
        equipment_picture = EquipmentPicture.objects.create(
            file=EquipmentPicture.get_default_file_path(),
            slug=EquipmentPicture.get_default_slug(), 
            bucket_name=r_user.company.s3_bucket.name,
            bucket=r_user.company.s3_bucket, added_by=request.user,
            company=r_user.company, url_with_bucket=url_bucket)                
        equipment.picture = equipment_picture
        equipment.save()
        return redirect('equipment_index')
   return render(request, 'equipment/add.html', {'equipment_form': form, 'company_id': r_user.company_id})

If I look at this, I see that we are rendering straight-away a template and passing it data. This would't work in iOS.

Few questions:

  1. I see a lot of people creating REST APIs. I don't really see the point of doing that if we can just create HTTPResponses with Django. If we were to use something like TastyPie, we wouldn't be able to just create an equipment (like we do right now) with a POST Statement as if you look at our current add function a lot of stuff is done within that function and TastyPie wouldn't be able to call that.

  2. My main question is should we have a REST API running as well as the normal Django server for both the Web and iOS platform, or just have the same functions, with different entry points and renderings according to it?

  3. When do you create a REST API? A lot of our functions when creating and getting data wouldn't work right now with just standard POST and GET calls. Is that bad?

I am kind of confused... sorry for the long question and thanks again!

4

1 回答 1

1

REST API 与 GET 和 POST 没有什么不同。它只是将它们用于数据而不是演示。您输出 JSON 或 XML 而不是标记代码。API 使用者然后为数据构建所需的用户界面。

REST API 的真正意义在于利用 HTTP 已经提供的所有工具,而不是发明另一层封装(如 SOAP)

例如,要指示某些操作的结果(成功与否),请使用状态码。要指示要执行的操作,通常使用动词(get、post、put、delete、head、options)。其他请求或响应元数据将进入适当的 http 标头。

这种方式数据更简单,缓存更容易,集成也很容易。

此外,借助良好的 REST API,您甚至可以构建一个网站,该网站使用一些 JavaScript 框架(例如,backbonejs)从 API 消耗的数据构建 UI。

更新

所以,考虑到 Django 中 REST 事物的当前状态,并且有一些生产和使用 API 的经验,我会告诉你构建:

  1. 一个 RESTful API 与您的 Web 一起使用,如果可能的话,使用您用于网站的相同控制器。同一个 Web 实际上是 RESTful,唯一的区别是它不是 API。我曾尝试在一些项目中使用多个库来构建 API,但最终总是为它生成我自己的代码。有时这样会更容易。所有客户端应用程序(iOS、android 等)都将使用您的 API,因此您可以将其完全解耦。

    如果您对自己构建所有 API 感到不自在,那么活塞可能是美味派的有效替代品。

  2. 只有一个 RESTful API,并使用客户端技术重建您的站点。当您的(许多)客户/用户的浏览器可以完成相同的工作而您不支付电费时,为什么要让您的(少数)服务器忙于生成 HTML?

在任何情况下,RESTful API 都将帮助您组织好应用程序的数据结构。

于 2012-10-20T19:00:55.680 回答