admin管理员组

文章数量:1192921

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.

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.

Share Improve this question edited Oct 7, 2013 at 16:25 dialex asked Feb 6, 2013 at 10:51 dialexdialex 2,8568 gold badges47 silver badges78 bronze badges 5
  • 1 For this behavior you need JavaScript that calls your django views which in turn calls a djangomodels method. – Henrik Andersson 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 ;) – dialex Commented Feb 6, 2013 at 11:04
  • Have a look at this nice example docs.djangoproject.com/en/dev/topics/class-based-views/… – dmg Commented Feb 6, 2013 at 15:21
  • I need the same functionality. How did you achieve it? – user2760685 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/) – dialex Commented Dec 8, 2013 at 17:23
Add a comment  | 

3 Answers 3

Reset to default 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.

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.

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

本文标签: pythonCan Javascript call a Django methodfunctionStack Overflow