2011-12-27 30 views
0

我是python新手,刚刚完成了django教程。我有一个Django支持的站点,我正在编写一个android应用程序,它需要从django管理的同一个数据库发送和接收数据。网站和应用程序提供相同的功能,但我不想为应用程序呈现页面,我只想发送/接收命令和序列化对象。基本上就是我需要做的是如何让django在收到http请求时运行python程序?

接收HTTP请求(从移动)在django ---> Run myProgram.py ---> Update database ---> send confirmation to client.

我能有什么/在哪里修改几个指针?谢谢。

回答

7

看来你正试图用Django制作一个API。您可以阅读更多关于REST API的here。基本的想法是,你的网站将有一组命令链接/ URL - 每个链接将执行一些操作(更新数据库等),只是返回一些信息(最常见的是JSON - 例如从数据库返回一个对象) ,或者将两者兼而有之。

你将不得不做的是列出所有可能的API命令将要处理的命令。这将包括检索,插入,更新和删除数据的所有命令。

对于此示例,我们假设应用程序的唯一工作是管理商店中的商品。这是一个非常简化的演示,但它应该得到重点。

这里是一个Django模型(内Store Django应用程序):

class Item(models.Model): 
    name = models.CharField(max_length=255) 
    price = models.IntegerField() # assume the price is always int 
    quantity = models.IntegerField() 

那么接下来让我们假设为API是可能的命令:

  • 获取有关特定项目的信息
  • 获取信息关于所有项目
  • 特定商品的更新信息
  • 删除特定项目
  • 添加物品到存储

URL结构中的所有命令

urlpatterns = patterns('', 
    # will be used for getting, updating, and removing an item 
    url(r'^item/(?P<item_id>\d+)/$', "item"), 
    # will be used to get info about all items 
    url(r'^item/all/$', "item_all"), 
    # will be used to create new item 
    url(r'^item/new/$', "item_new"), 
) 

获取信息有关的所有项目

为了从django获取信息信息,django序列化函数非常有用。 Here是一个django文档。

#views.py 
from django.core import serializers 
import json 

def item_all(request): 
    items = Item.objects.all() 
    data = serializers.serialize("json", items) 
    return HttpResponse(data, mimetype="application/json") 

这将完成是返回一个JSON阵列,像这样所有的项目:

[ 
    { 
    pk: 1, 
    model: "store.item", 
    fields: { 
     name: "some_name", 
     price: 20, 
     quantity: 1000 
    } 
    }, 
    // more items here 
] 

添加新项

对于这个您的Android应用程序将有发送一个JSON对象给Django。

JSON对象:

{ data: 
     { 
      name: "some_name", 
      price: 40, 
      quantity: 2000 
     } 
} 

所以,现在你的Django应用程序必须分析来自请求的信息,以创建一个新项目:

#views.py 

def item_new(request): 
    # the request type has to be POST 
    if request.method != "POST": 
     return HttpResponseBadRequest("Request has to be of type POST") 
    postdata = request.POST[u"data"] 
    postdata = json.loads(postdata) 
    item = Item() 
    item.name = postdata["name"] 
    item.price = postdata["price"] 
    item.quantity = postdata["quantity"] 
    item.save() 
    data = serializers.serialize("json", [item]) 
    return HttpResponse(data, mimetype="application/json") 

一些注意事项:

注使用POST请求类型。这在REST API中非常重要。基本上取决于请求类型,将执行不同的操作。在接下来的观点中这将更加生动。


获取,更新和删除

#views.py 

def item(request, item_id): 
    item = get_object_or_404(Item, pk=item_id) 
    if request.method == "GET": 
     data = serializers.serialize("json", [item]) 
     return HttpResponse(data, mimetype="application/json") 
    elif request.method == "POST": 
     postdata = request.POST[u"data"] 
     postdata = json.loads(postdata) 
     item.name = postdata["name"] 
     item.price = postdata["price"] 
     item.quantity = postdata["quantity"] 
     item.save() 
     data = json.dumps(True) 
     return HttpResponse(data, mimetype="application/json") 
    elif request.method == "DELETE": 
     item.delete() 
     data = json.dumps(True) 
     return HttpResponse(data, mimetype="application/json") 
    else: 
     return HttpResponseBadRequest("Invalid request") 

那么这种方法将是根据不同的请求类型做不同的动作。


评论

请注意,这是一个非常,非常简单的演示。它没有考虑任何错误检查,用户认证等。但希望它会给你一些想法。

相关问题