ajax - 尝试导入 json 时如何修复 django 中的 ImportError?
问题描述
我有一个网页,其中包含 2 个链接的下拉列表国家和城市,其中基于第一个下拉列表的选择,第二个下拉列表显示城市属于所选国家/地区。
问题是第一个下拉列表显示从数据库中获取的数据,但第二个仍然是空的。并且系统显示以下错误:
from django.utils import json as simplejson ImportError: cannot import name 'json' from 'django.utils'
模型.py
from django.db import models
class country(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return str(self.name)
class city(models.Model):
name = models.CharField(max_length=100)
MouhafazatID = models.ForeignKey(country,on_delete=models.CASCADE)
def __str__(self):
return str(self.name)
网址.py
from django.contrib import admin
from django.urls import path, include
from.views import *
urlpatterns = [
path('admin/', admin.site.urls),
# path('', home),
path('', home2),
path('getdetails/', getdetails),
视图.py
from django.shortcuts import render
from django.http import HttpResponse
from testapp.models import *
from django.utils import json as simplejson # i think this is the error?
def home2(request):
countries = country.objects.all()
print(countries)
return render(request, 'home2.html',{'countries': countries})
def getdetails(request):
#country_name = request.POST['country_name']
country_name = request.GET['cnt']
print ("ajax country_name ", country_name)
result_set = []
all_cities = []
answer = str(country_name[1:-1])
selected_country = country.objects.get(name=answer)
print ("selected country name ", selected_country)
all_cities = selected_country.city_set.all()
for city in all_cities:
print ("city name", city.name)
result_set.append({'name': city.name})
return HttpResponse(simplejson.dumps(result_set), mimetype='application/json', content_type='application/json')
最后一行出现错误如何解决?
home2.html
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="http://yourjavascript.com/7174319415/script.js"></script>
<script>
$(document).ready(function(){
$('select#selectcountries').change(function () {
var optionSelected = $(this).find("option:selected");
var valueSelected = optionSelected.val();
var country_name = optionSelected.text();
data = {'cnt' : country_name };
ajax('/getdetails',data,function(result){
console.log(result);
$("#selectcities option").remove();
for (var i = result.length - 1; i >= 0; i--) {
$("#selectcities").append('<option>'+ result[i].name +'</option>');
};
});
});
});
</script>
</head>
<body>
<select name="selectcountries" id="selectcountries">
{% for item in countries %}
<option val="{{ item.name }}"> {{ item.name }} </option>
{% endfor %}
</select>
<select name ="selectcities" id="selectcities">
</select>
</body>
</html>
解决方案
我能够通过更改
from django.utils import json as simplejson
至
import json as simplejson
推荐阅读
- powershell - 如何在powershell脚本中传递一些参数来运行.bat文件?
- c++ - 如何在 C++ 中正确重载运算符 <<?
- python - 我试图做一个密码系统。但我无法从用户那里获得条目,谁能解释我为什么?
- image - 有没有办法在加载屏幕期间缓存所有网络图像?扑
- jquery - 使用 jquery 突出显示文本
- firebase - 将选取的图像上传到 Firestore 存储
- mobile-safari - iOS 15 Safari CSS
- c# - 平行装配线平衡问题的模拟退火
- mongodb - MongoDB E11000 dup key on insertMany 但不在 insertOne 上的空集合上,默认索引
- javascript - Flask 应用程序运行两次