In this post, I will continue from where my first post about ionic app with Django backend. In part 2, I will talk more about the front-end side of things i.e. the ionic app. I have a background in web development, hence I call this front-end, but that’s not the most accurate thing to say in these modern times. So the right way to say it is, I will talk about the Ionic app found at this, Github repository(IonicAppWithRestBackend) in more details in this post.

Ionic app with django, recap?

In part 1, we got to the point of running our backend on the web server that ships with Django. To refresh your memory, this is how we got the backend server running,
Clone the repo
git clone https://github.com/cptdanko/IonicAppWithRestBackend
Navigate to the backend project directory
cd IonicAppWithRestBackend/backend/python_django/restBackend/
Note: if you are using Windows, you may want to change “/” to “” in the above command.
Once there you can run the backend server with the following command
python manage.py runserver
At this point we should have out backend running on http://localhost:8000/, which exposes two RESTful endpoints that can be accessed like this http://localhost:8000/smugglers/ and http://localhost:8000/smuggler/id/ I will talk more about what these end points do a bit later, but for now lets talk about the Ionic app. So how do we run the Ionic app? To run the Ionic app in this project, in the folder where you cloned the repo i.e. IonicAppWithRestBackend, navigate to the following directory
cd IonicAppWithRestBackend/ionicApp/
Note: Windows users watch out for the “/” and run the following command
ionic serve
This will start the Ionic app and you can test it in the browser using at http://localhost:8100. What is the Ionic app in this repo? The Ionic app is a simple sidemenu app which fetches some data from the backend and displays it . The screenshot on the right shows what the app looks like. When you run the app, it fetches some data(names of 2 smugglers) from the http://localhost:8000/smugglers/  endpoint from our backend. Data is fetched using the $http service in AngularJS. In addition to the official AngularJS docs, you can also look at the tutorial: Integrating a Backend Service with AngualrJS on Learn Ionic. If you click on Hondo or Han the app will use the id of the smuggler to fetch more details about him from this endpoint http://localhost:8000/smuggler/id/ and show that info using an Ionic popup. p.s. regarding the data, hmmm…let’s just say, I am looking forward to Dec 2015!

Get updates?

Any other problem solved?

Since both our Ionic app and the RESTful backend are running on localserver, I ran into the CORS issue when I first started working on this. Thankfully this blog post on the Ionic blog provides a solution to this owing to which the ionic.project file for the Ionic app in this repo(IonicAppWithRestBackend) looks like this,
"proxies": [
    {
        "path":"/api/",
        "proxyUrl": "http://localhost:8000/"
    }
]
Now, if you look at the code in HomeCtrl.js in this repo, the urls we pass to the $http service look something like /api/smugglers or ‘/api/smuggler/’+smugler.id+’/’; That combined with the proxy that we have in our ionic.project file means that every time our app is calling a url which has api in it’s path, the api part of the url will be replaced with http://localhost:8000/   so when we call(invoke) /api/smugglers/ thanks to our proxy, the actual url invoked will look like this http://localhost:8000/smugglers/

Conclusion

None actually! Like I said in part 1, should you have any questions about this or suggestions on how I can improve this repo, then please leave a comment.

Get updates?

Get updates?

As usual, if you find any of my posts useful support me by  buying or even trying one of my apps on the App Store.  https://mydaytodo.com/apps/ Also, if you can leave a review on the App Store or Google Play Store, that would help too.
Categories: Ionic

0 Comments

Leave a Reply

Avatar placeholder
Verified by MonsterInsights