JordanChina
JordanChina

Reputation: 345

How can I build a website with template and rest api for mobile app in Django

I want to build a project with Django framework, it includes website and mobile app, website is built based on Django template, and mobile app needs rest api. How can I do it with only one copy of source code?

In other words, can I create one project, in which there are several django apps, it supports returning both rendering template and json for mobile app?

I know we can use Angular JS in website, then both website and mobile app access rest api build with Django-Rest-Framework. But I have no idea about Angular JS and there is no time for me to learn it.

Please help.

Upvotes: 0

Views: 1540

Answers (3)

Cajetan Rodrigues
Cajetan Rodrigues

Reputation: 172

This is easy to achieve if you leverage the power of HTTP headers, specifically speaking the "Content-Type" header (HTTP 1.1 headers specification)

The way I personally use it is something like this:

def my_view_name(request):

    if 'CONTENT_TYPE' in request.META and \
        request.META['CONTENT_TYPE'] == 'application/json':
            return HttpResponse(
                json.dumps({"foo":"bar"}), 
                content_type='application/json'
            )
    else:
        return render_to_response(
            'sometemplate.html', context
        )

The code above is django-specific.

It allows you to control what kind of response the server doles out, based out what the client passes in. It can detect that a JSON response is needed by checking the Content-Type header. Just ensure your mobile app makes its HTTP requests with the Content-Typeheader set to application/json

Hope that's what you wanted.

Upvotes: 5

SuperNova
SuperNova

Reputation: 27466

You can build one Common Backend to serve your data.

And you can do this without having a Django-rest-Framework(without having to use angular). You are just going call the url of the backend.

For eg.,. lets say backend code is running at 127.0.0.1/8000, and you need to get a user's details, you are just going to call url 127.0.0.1:8000/get_user_details/ from mobile app or any other front end.

And please make a habit of forming/arranging data written in a different function so that it is reusable by both the Mobile app and the website.(plz see below)

###  For browser website written in DJANGO (served in template by django):
def user_details_page(request):
    get_user_details(someid)    #### returns json of user details
    render in template

#### And for the mobile App you could use **get_user_details(someid)** just to get json data.

def get_user_details(someid):
   ###do watever
   return json ## of user details

And you can also use Django Rest Framework(without having to use angular), it gives you a lot of stability.

Or you can setup a entire new backend in Django Framework to serve data for mobile App.

Upvotes: 1

shpasta
shpasta

Reputation: 1933

You can do it in one project. Have the same Model, but have different views for website and REST-api.

Upvotes: 1

Related Questions