4

Imagine a simple page with a counter and two buttons. The value displayed by the counter is a read/stored in a model's field. I want that when I press the "green" button the counter is incremented by one and when I press the "red" button the counter is decreased by one. What's the best way to implement such behavior:

  1. Button calls model's method (Django method) which updates the model's field (DB write); Entire page is refreshed and the counter display updated (DB read).
  2. Button calls javascript function which updates the counter display (JS/HTML); In the background, a model's method (Django method) is called to update the model's field (DB write).
  3. Yet another way?

Can the javascript code call a Django function? I'm a newbie at Django (I followed the tutorial up to part 4). I already understood the MVC/MTV concept and the data read/write/display, but what's bothering me now is introducing behavior/interactivity on my pages.

5
  • 1
    For this behavior you need JavaScript that calls your django views which in turn calls a djangomodels method. Commented Feb 6, 2013 at 11:00
  • You should post that comment as an answer, so I can vote it. I think you got it right ;) Commented Feb 6, 2013 at 11:04
  • Have a look at this nice example docs.djangoproject.com/en/dev/topics/class-based-views/… Commented Feb 6, 2013 at 15:21
  • I need the same functionality. How did you achieve it? Commented Dec 8, 2013 at 13:34
  • @user2760685 I can't answer your question since after a while I gave up on Django and switched to Play Framework (www.playframework.com/) Commented Dec 8, 2013 at 17:23

3 Answers 3

20

JavaScript in browser-side is sitting on the front end, while Django is serving on the backend(server-side). The former can neither nor need to directly call the latter's functions. The interface between them is typically web service APIs, namely, browser that makes AJAX calls with URLs defined in web services, which are backed by Django.

More specifically, JavaScript sends HTTP requests to web server, in turn Django's URL dispatcher maps the request URLs to corresponding Django views(function-based or class-based). In short, a typical route can be simplified as:

JavaScript -> HTTP request -> Django URL dispacher(mapping rules are in urls.py or urls/XXX.py) -> Django view function(views.py or views/XXX.py) -> Django form(optional) -> Django model(optional).

For more Django technical details, you may refer to Django tutorial or Practical django Projects.

Final word: even if JavaScript could call Django function method/function, it should be avoided. From web service's perspective, Django methods/functions are only implementation detail, which are more subject to change(compared to web service API). Backend developers may change function name, switch to some framework other than Django, or even change programming language like Java, Ruby or PHP for whatever reason.

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

1 Comment

In that case how can I introduce behavior? By specific private urls which call Django methods?
4

JavaScript runs in the browser, whereas Django runs on the server. Browsers communicate with servers using HTTP.

You can make an HTTP call from JavaScript using the XMLHttpRequest API. (This is often referred to as AJAX.) You can send an HTTP GET request (the same request a browser sends when you click a link) or an HTTP POST request (the same request a browser sends when you submit some forms) to a URL on the server.

You set Django up to handle this URL via urls.py, as I'm sure you know from the tutorial.

Comments

1

Use AJAX, jQuery or dajax makes it nice and easy.

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.