Another small project with Django/ajax/mysql/highcharts. Look at-delivery dashboard. Hey, isn't it pretty?
Don't say much nonsense. Summary of issues encountered in the process:
1. Ajax URL Request 403 Error:
is the cause of CSRF (Cross-site request forgery cross-site requests for forgery protection). There are currently three ways
1.1 I am using @csrf_exempt in front of the corresponding view, but this is not safe, is to turn off the CSRF protection function.
(View in the package, setting added middleware_classes
view.py: from Import csrf_exemptsetting.py: ' Django.middleware.csrf.CsrfViewMiddleware ',
1.2 Seems to be in the template submission area Riga {% Csrf_token%}? Not particularly clear, I first built, This later study, pending
1.3 Slightly
2. Mysql/json issue with date format
Because the date format that Django reads from MySQL is Datetime.date (2015, 4, 28), it is problematic to convert to JSON to the foreground or to other operations.
def Date_handler (obj): return if ' Isoformat ' Else The obj#hasattr () method is to find whether the property is included, returned as a Boolean type
Simplejson.dumps ( array , default=date_handler)
Simplejson.loads ( array )
The above Date.isoformat () means a string that returns a format such as ' Yyyy-mm-dd '. Because Python does not have the Apply function like R, the JSON function is currently used to convert the data in the array into
The string ' Yyyy-mm-dd '
3. Use 2 different databases:
Set in setting, such as adding a new other (using the test database): When running raw_query in view,
<table_name>.objects.using ('other' )
DATABASES = { 'default': { 'ENGINE':'Django.db.backends.mysql', 'NAME':'Alert', 'USER':'Root', 'PASSWORD':'1234', 'HOST':"', 'PORT':'3306', }, ' Other': { 'ENGINE':'Django.db.backends.mysql', 'NAME':'Test', 'USER':'Root', 'PASSWORD':'1234', 'HOST':"', 'PORT':'3306', }}
4.highcharts/ajax Data transfer problem
1. Except for strings, which seem to be structured like arrays, dictionaries are not uploaded to the foreground, except JSON data structures. So there is no way back view using JSON.DUMP data structure first.
The {{variable |safe}} is then used in the foreground to pass it to the front end.
2. Ajax/highcharts Dynamic Refresh
Because Render_to_response can only return HTML data for the Hcharts of the JavaScript framework is not good operation. Here we can use HttpResponse
from Import HttpResponse
Return HttpResponse (<json_data>,content_type='application/json')
Think the more magical thing is the highcharts is directly in the JSON data format, before I use Jsonstrify to convert to string has not been, and finally directly into the home! Then Can To!
Front desk Js/ajax:
$.post ("/cn_aj_res/", {"P":p},function(data) {$ (' #container-top '). Highcharts (data);});
Backstage view.py:
cn_json={' title ': {' text ': ',}, ' Xaxis ': {' categories ': variable , ' Tickinterval ': 1}, ' YAxis ': {' title ': {' text ': ' Missing SLA Silo Number '}, ' min ': 0, ' Max ': $, ' plotlines ': [{' Value ': 0, ' width ': 1, ' Color ': ' #808080 ',}]}, ' legend ': {' Layout ': ' Vertical ', ' align ': ' Right ', ' verticalalign ': ' Middle ', ' BorderWidth ': 0}, ' series ': [{' Name ': ' Include Weekend ', ' data ': variable },{' name ': ' Exclude Weekend ', ' data ': variable }]}
#注意此处的变量不可以是JSON格式的, otherwise the whole cn_json turns into JSON, there is a problem, if the variable before is JSON, first json.loads into a normal
Cn_json = Simplejson.dumps (Cn_json)
Return HttpResponse (cn_json,content_type= ' Application/json ')
Django Ajax MYSQL Highcharts<1>