gpt4 book ai didi

jquery - 使用 django 和 jquery 创建依赖下拉菜单时遇到问题

转载 作者:行者123 更新时间:2023-12-01 03:45:19 25 4
gpt4 key购买 nike

所以我找到了this关于如何制作两个依赖的下拉列表。我按照示例操作,现在当我尝试使用下拉菜单加载页面时,出现此错误:

AttributeError at /brewkeep/beers/
'dict' object has no attribute 'status_code'

这是回溯:

Environment:


Request Method: GET
Request URL: http://192.168.1.111:8080/brewkeep/beers/

Django Version: 1.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'brewkeep',
'django.contrib.admin')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/home/di/djangostack-1.4-1/apps/django/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
188. response = middleware_method(request, response)
File "/home/di/djangostack-1.4-1/apps/django/lib/python2.7/site-packages/django/middleware/common.py" in process_response
94. if response.status_code == 404:

Exception Type: AttributeError at /brewkeep/beers/
Exception Value: 'dict' object has no attribute 'status_code'

这是我的代码:

urls.py

(r'^brewkeep/beers/$', 'brewkeep.templatetags.brewery_beer_select'),
(r'^brewkeep/beers/(?P<brewery_id>[-\w]+)/all_json_models/$', 'brewkeep.views.beer_views.show_beers'),

models.py

class Brewery(models.Model):
idbrewery = models.AutoField(primary_key=True)
name = models.CharField(max_length=765)
class Beer(models.Model):
idbeer = models.AutoField(primary_key=True)
brewery = models.ForeignKey(Brewery, null=True, on_delete=models.SET_NULL)

templatetags.py

from brewkeep.models import Brewery
from django import template

register = template.Library()

@register.inclusion_tag("show_beers.html")
def brewery_beer_select(request):
breweries = Brewery.objects.all().order_by('name')
return {'breweries' : breweries}

show_beers.html

<form action="" method="get" accept-charset="utf-8">
<p>Brewery:</p>
<select name="brewery" id="brewery">
<option value="">Select a brewery</option>
{% for brewery in breweries %}
<option value="{{brewery.idbrewery}}">{{brewery.name}}</option>
{% endfor %}
</select>
<select name="beer" id="beer" disabled="true">
<option>Select a beer</option>
</select>
</form>
<script src="{{ STATIC_URL }}admin/js/jquery.min.js">
$(document).ready(
function() {
$("select#brewery").change(function() {
if ($(this).val() == '') {
$("select#beer").html("<option>Select a beer</option>");
$("select#beer").attr('disabled', true);
}
else {
var url = "/brewkeep/beers/" + $(this).val() + "/all_json_models";
var brewery = $(this).val();
$.getJSON(url, function(beers) {
var options = '<option value="">Select a beer</option>';
for (var i = 0; i < beers.length; i++) {
options += '<option value="' + beers[i].pk + '">' + beers[i].fields['description'] + '</option>';
}
$("select#beer").html(options);
$("select#beer option:first").attr('selected', 'selected');
$("select#beer").attr('disabled', false);
});
}
});


$("select#beer").change(function(vent) {
if ($(this).val() == '') {
return;
}
});
}
);

</script>

beer_views.py

def show_beers(request, brewery_id):
brewery_obj = Brewery.objects.get(idbrewery=brewery_id)
beers = Beer.objects.all().filter(brewery=brewery_obj).order_by('name')
json_models = serializers.serialize("json", beers)
return HttpResponse(json_models, mimetype="application/javascript")

知道我错过了什么吗?

谢谢!

最佳答案

为了满足您的要求,我喜欢使用 django-smart-selects 。它包含 js 功能,可将模型与名为 ChainedForeignKey 的模型字段链接起来。例如,如果您想要一个电话链接选择,用户可以在其中选择电话类型、品牌和型号(例如 Iphone 4 S 或 Samsung Galaxy 2),则它看起来像这样:

模型.py:

from smart_selects.db_fields import ChainedForeignKey

class Series(models.Model):
series = models.CharField(max_length=10)

class Model(models.Model):
model = models.CharField(max_length=20)
series = models.ForeignKey(Series)

class Make(models.Model):
make = models.CharField(max_length=20)
model = models.ForeignKey('Model')

class Phone(models.Model):
make = models.ForeignKey(Make)
model = ChainedForeignKey(Model, chained_field='make', chained_model_field='make',)
series = ChainedForeignKey(Series, chained_field='model', chained_model_field='model',)

class PhoneForm(ModelForm):
class Meta:
model=Phone

模板.html:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

{% block content %}
<form action='' method='post' enctype='multipart/form-data'>
{{ form.as_p }}
{% csrf_token %}
<input type='submit' value='submit' />
</form>
{% endblock %}
<小时/>

编辑:基于评论

来自文档

The chained field is the field on the same model the field should be chained too.The chained model field is the field of the chained model that corresponds to the model linked too by the chained field.

更改为chained_model_field='name'

关于jquery - 使用 django 和 jquery 创建依赖下拉菜单时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14150625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com