1

I've seen some questions around this topic, though I still didn't find the answer I'm looking for.

What is the right way to implement AngularJS application to interact with django?

As I understand the plan should like:

  1. Use django-restful to develop some api. it will be available e.g. /api/v1/whatever/app
  2. Right separate AngularJS code which will interact with api from step 1.
  3. Do the magic: Django is available with django router starting from / . How AngularJS pages should be loaded to client? Should I write additional TemplateView to allow angularjs part load on client side for /? How full urls (e.g. /post/ will be routed)? As I understand there is separate router in angularjs. Should it be separated on web server part? E.g. /api is handled by nginx to django and / is handled to static to load angular?

Do I need to use django-angular at all? As probably it looks like it would be better if angular won't be integrated into django templates and it will be completely separated. Is it so?

Maybe it's a bit mixed up in my head. Though I didn't find related explanation here.

1
  • If you will use Django to build an API, you need just use the endpoints, you just need to return JSON objects from the API and use the data in your AngularJS app in front-end. You can use a SPA, or any other structure to your AngularJS app, and, of course, use the AngularJS routing. In the architecture, Django is a data provider. Commented Oct 21, 2015 at 20:21

1 Answer 1

3

I have been using Django Rest Framework and AngularJS with success, and heres how I've done it:

  1. Serve the AnguarJS as a static file from www.yourdomain.com. This is easily done with nginx or apache.
  2. Next, access the Django Rest Framework through api.yourdomain.com, also an easy configuration.

You could also have Django serving the static files (AngularJS), but I believe its an unnecessarily complicated approach.

If you implement this solution, be aware of cross-CORS errors. This happens when your angularJS (from www.yourdomain.com) tries to access a service (Django Rest Framework) from another domain (api.yourdomain.com). This issue can easily be fixed using a few lines of extra server configuration (nginx example)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.