所以我发现了这个关于如何制作两个相关下拉菜单的信息。我按照示例进行操作,现在当我尝试使用下拉菜单加载页面时,出现此错误:
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'
这是我的代码:
网址.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'),
模型.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)
模板标签.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")
知道我错过了什么吗?
谢谢!