導航:首頁 > 編程語言 > pythondjangojson

pythondjangojson

發布時間:2022-06-24 23:55:13

python django通過ajax向後端傳json怎麼解析

這么有逼格的問題竟然沒分!

$.ajax({
url:"xxx",
dataType:'json',
data:formData,
type:"POST",
cache:false,
processData:false,
contentType:false
})
.done(function(data){
varcode=data.err_code;
if(code.length!==0){
switch(code){
case'10001':
layer.msg(loginTimeoutText);
self.locatToLogin();
break;
case'10003':
layer.msg(systemBusyText);
break;
case'0':
location.href="/xxx/?id="+id+"&vote_id="+data.id;
break;
case'9999':
layer.msg(notLoginedInText);
//self.locatToLogin();
break;
default:
break;
}
}
})
.fail(function(){
layer.msg(failText);
});

❷ python3.6下如何用Django1.9創建資料庫報錯

錯誤描述
python==3.5
django==1.7
django創建項目時報錯如下:
Traceback (most recent call last):
File "/root/envs/django-test/bin/django-admin", line 11, in <mole>
sys.exit(execute_from_command_line())
File "/root/envs/django-test/lib/python3.5/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/root/envs/django-test/lib/python3.5/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/root/envs/django-test/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
from django.utils.log import configure_logging
File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/log.py", line 10, in <mole>
from django.views.debug import ExceptionReporter, get_exception_reporter_filter
File "/root/envs/django-test/lib/python3.5/site-packages/django/views/debug.py", line 10, in <mole>
from django.http import (HttpResponse, HttpResponseServerError,
File "/root/envs/django-test/lib/python3.5/site-packages/django/http/__init__.py", line 4, in <mole>
from django.http.response import (HttpResponse, StreamingHttpResponse,
File "/root/envs/django-test/lib/python3.5/site-packages/django/http/response.py", line 13, in <mole>
from django.core.serializers.json import DjangoJSONEncoder
File "/root/envs/django-test/lib/python3.5/site-packages/django/core/serializers/__init__.py", line 23, in <mole>
from django.core.serializers.base import SerializerDoesNotExist
File "/root/envs/django-test/lib/python3.5/site-packages/django/core/serializers/base.py", line 6, in <mole>
from django.db import models
File "/root/envs/django-test/lib/python3.5/site-packages/django/db/models/__init__.py", line 6, in <mole>
from django.db.models.query import Q, QuerySet, Prefetch # NOQA
File "/root/envs/django-test/lib/python3.5/site-packages/django/db/models/query.py", line 13, in <mole>
from django.db.models.fields import AutoField, Empty
File "/root/envs/django-test/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 18, in <mole>
from django import forms
File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/__init__.py", line 6, in <mole>
from django.forms.fields import * # NOQA
File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/fields.py", line 18, in <mole>
from django.forms.utils import from_current_timezone, to_current_timezone
File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/utils.py", line 15, in <mole>
from django.utils.html import format_html, format_html_join, escape
File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/html.py", line 16, in <mole>
from .html_parser import HTMLParser, HTMLParseError
File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/html_parser.py", line 12, in <mole>
HTMLParseError = _html_parser.HTMLParseError
AttributeError: mole 'html.parser' has no attribute 'HTMLParseError'
(django-test) root@localhost:~/source/djangotest# django-admin startproject superlists
Traceback (most recent call last):
File "/root/envs/django-test/bin/django-admin", line 11, in <mole>
sys.exit(execute_from_command_line())
File "/root/envs/django-test/lib/python3.5/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/root/envs/django-test/lib/python3.5/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/root/envs/django-test/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
from django.utils.log import configure_logging
File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/log.py", line 10, in <mole>
from django.views.debug import ExceptionReporter, get_exception_reporter_filter
File "/root/envs/django-test/lib/python3.5/site-packages/django/views/debug.py", line 10, in <mole>
from django.http import (HttpResponse, HttpResponseServerError,
File "/root/envs/django-test/lib/python3.5/site-packages/django/http/__init__.py", line 4, in <mole>
from django.http.response import (HttpResponse, StreamingHttpResponse,
File "/root/envs/django-test/lib/python3.5/site-packages/django/http/response.py", line 13, in <mole>
from django.core.serializers.json import DjangoJSONEncoder
File "/root/envs/django-test/lib/python3.5/site-packages/django/core/serializers/__init__.py", line 23, in <mole>
from django.core.serializers.base import SerializerDoesNotExist
File "/root/envs/django-test/lib/python3.5/site-packages/django/core/serializers/base.py", line 6, in <mole>
from django.db import models
File "/root/envs/django-test/lib/python3.5/site-packages/django/db/models/__init__.py", line 6, in <mole>
from django.db.models.query import Q, QuerySet, Prefetch # NOQA
File "/root/envs/django-test/lib/python3.5/site-packages/django/db/models/query.py", line 13, in <mole>
from django.db.models.fields import AutoField, Empty
File "/root/envs/django-test/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 18, in <mole>
from django import forms
File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/__init__.py", line 6, in <mole>
from django.forms.fields import * # NOQA
File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/fields.py", line 18, in <mole>
from django.forms.utils import from_current_timezone, to_current_timezone
File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/utils.py", line 15, in <mole>
from django.utils.html import format_html, format_html_join, escape
File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/html.py", line 16, in <mole>
from .html_parser import HTMLParser, HTMLParseError
File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/html_parser.py", line 12, in <mole>
HTMLParseError = _html_parser.HTMLParseError
AttributeError: mole 'html.parser' has no attribute 'HTMLParseError'5767757677

原因
HTMLParseError在pythons3.5已經沒有了
解決方法
將python版本回退到3.3或3.4
升級django版本

❸ django model如何轉換成json

直接利用python提供的json包,在django model的定義中增加一個方法toJSON,利用django model 能訪問 _meta.fields 得到相關屬性而得到,例子如下:

classCategory(models.Model):
autoid=models.AutoField(primary_key=True)
email=models.CharField(max_length=150,blank=False)
comtype=models.CharField(max_length=20,blank=False)
catname=models.CharField(max_length=150,blank=False)

def__unicode__(self):
return'%s'%(self.catname)

deftoJSON(self):
importjson
returnjson.mps(dict([(attr,getattr(self,attr))forattrin[f.nameforfinself._meta.fields]]))

然後用django查出數據,並轉換成json,代碼如下:

row=models.Category.objects.get(autoid=23)
printrow.toJSON()

❹ django views怎麼返回json

直接利用python提供的json包,在django model的定義中增加一個方法toJSON,利用django model 能訪問 _meta.fields 得到相關屬性而得到,例子如下:

class Category(models.Model):
autoid = models.AutoField(primary_key=True)
email=models.CharField(max_length=150,blank=False)
comtype=models.CharField(max_length=20,blank=False)
catname=models.CharField(max_length=150,blank=False)

def __unicode__(self):
return '%s' % (self.catname)

def toJSON(self):
import json
return json.mps(dict([(attr, getattr(self, attr)) for attr in [f.name for f in self._meta.fields]]))

然後用django查出數據,並轉換成json,代碼如下:

row=models.Category.objects.get(autoid=23)
print row.toJSON()

閱讀全文

與pythondjangojson相關的資料

熱點內容
php獲取上個月時間戳 瀏覽:574
怎麼設置oppo手機app密碼 瀏覽:360
jra格式下載哪個文件夾 瀏覽:658
蘋果手機怎麼刪除手機上的app 瀏覽:343
單片機熱敏電阻溫度控制 瀏覽:82
方舟私人伺服器的服主怎麼刷物資 瀏覽:454
重裝系統提示加密鎖 瀏覽:625
為什麼下載的音樂要加密 瀏覽:964
因為沒有打開的文檔所以這一命令無效 瀏覽:720
電腦文件夾數據損壞 瀏覽:716
java打賞源碼搭建 瀏覽:365
pdf32 瀏覽:256
植發加密100天效果 瀏覽:959
離心壓縮機檢修規程 瀏覽:432
如何收聽安卓手機發來的鏈接 瀏覽:401
福建微法院app怎麼用 瀏覽:210
android陀螺儀應用 瀏覽:721
新加坡加密貨幣騙局 瀏覽:912
電腦文件夾打開自動變為全屏 瀏覽:971
簡易單斷面計演算法 瀏覽:852