Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6985441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:40:50+00:00 2026-05-27T18:40:50+00:00

I’m new to python and just finished the django tutorial. I have a django

  • 0

I’m new to python and just finished the django tutorial. I have a django powered site and I’m writing an android application which needs to send and receive data from the same database managed by django. The website and the application provide the same functionality, but I don’t want pages rendered for the application, I just want to send/receive commands and serialised objects. Essentially what I need to do is

receive http request (from mobile) in django ---> Run myProgram.py ---> Update database ---> send confirmation to client.

Could I have a few pointers about what/where to edit? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:40:50+00:00Added an answer on May 27, 2026 at 6:40 pm

    It seems that you are trying to make an API with Django. You can read here more about REST APIs. The basic idea is that your web site will have a set of command links/urls – where each link will either do some action (update db, etc), just return some information (most commonly in JSON – e.g. return an object from db), or will do both.

    What you will have to do is make a list of all possible commands the api will have to handle. That will include all the commands of retrieving, inserting, updating, and deleting data.

    For this example, lets assume the only job of the application is to manage items in a store. This is a very simplified demo however it should get the point across.

    Here is a Django model (within Store django app):

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

    So then lets assume the possible commands for api are:

    • Get information about specific item
    • Get information about all items
    • Update information for specific item
    • Remove specific item
    • Add item to store

    URL structure for all the commands

    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"),
    )
    

    Getting info about all items

    For getting information information from django, django serialization functions are very helpful. Here is a django docs for that.

    #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")
    

    What this will do is return a JSON array with all the items like so:

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

    Adding new item

    For this your android app will have to send a JSON object to the django.

    JSON object:

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

    So now your django app has to parse the info from the request in order to create a new item:

    #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")
    

    Some notes:

    Note the use of the POST request type. This is very crucial in REST API’s. Basically depending on the request type, different actions will be done. This is going to be much more vivid in the next view.


    Getting, updating, and removing

    #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")
    

    So what this method will be is do different actions according to the different request type.


    Comments

    Note that this is a very, very simple demo. It does not account for any error checking, user authentication, etc. But hopefully it will give you some ideas.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.