1
votes

I am terribly confused about the routing and url set up when using backbone.js and django together with the Django-restframework.

  1. Where does the the template fit in when using a REST framework?

For example, i have a class based view defined for one of my urls where i want to use backbone.js to just update the div displaying students:

url(r'^home/students/$', views.StudentList.as_view()),

class StudentList(APIView):
    """
    List all students
    """
    def get(self, request, format=None):
        students = Person.objects.filter(person_type = Person.STUDENT)
        serializer = PersonSerializer(students)
        return Response(serializer.data, "core/teachers/teacher_teaching.html")

    def pre_save(self, obj):
        obj.owner = self.request.user
  • How does Backbone routes fit in with the url routing of django. I have in a file router.js, something like this:

    function($,jqueryui, _, Backbone, HomeView, StudentsView) {
    
    var AppRouter = Backbone.Router.extend({
        routes: {
            // Define some URL routes
            ':home': 'showStudents',
            'users': 'showContributors',
    
            // Default
            '*actions': 'defaultAction'
        }
    });
    
    var initialize = function(){
    
        var app_router = new AppRouter;
    
        app_router.on('route:showStudents', function(){
    
            // Call render on the module we loaded in via the dependency array
            var studentsView = new StudentsView();
            studentsView.render();
        });
    

Yet the routes never actually get to my views?

1

1 Answers

6
votes

Where does the the template fit in when using a REST framework?

It doesn't; Django templates are for normal Django HTML pages, not REST framework APIs. REST framework API responses are programmatically-generated documents (typically JSON or XML), so they don't need templates.

How does Backbone routes fit in with the url routing of django

They are 100% completely separate. Let's say you have a URL like:

www.example.com/foo/bar#baz

Django will handle this much of that URL:

www.example.com/foo/bar

while Backbone will handle the remaining:

#baz

Of course, that assumes that Django returns a webpage that uses Backbone; if it doesn't, the #baz will never even come in to play.

As a quick summary, when you visit a URL like the above, your browser will ask www.example.com/ for /foo/bar. This is when your urls.py in Django comes in; it has:

url(r'^foo/bar/?$', views.FooBar.as_view()),

so your webserver knows to send back whatever views.FooBar.as_view() gives it (whether that is REST framework-produced JSON document or an old school Django HTML document).

Once that document comes back to the server, it renders it, and this is where client-side code like Backbone and its router take their turn. The Backbone Router (whenever you call Backbone.history.start()) looks at the URL (ie. window.location) and looks for the anchor or "hash" part of the URL (eg. #baz). It then compares that part of the URL to its route list (similar to how your webserver compared the earlier part to urls.py) and if it finds a match, it "sends you to a page".

It's key to understand though, it doesn't really send you anywhere at all; all its really doing is changing the hash part of the URL and then running some Javascript which manipulates the DOM. Therefore, it is impossible for the Backbone Router to ever send you to a Django URL; if you want to hit a server route, you need to either use AJAX or window.location =.