我被困在第19章与Django探戈。我只是试着按照书中的“添加内联类别建议”代码来做。但是当我输入python时,什么也没发生。你知道吗
这是我的密码
你知道吗基本.html你知道吗
<ul class="nav nav-list">
<li class="nav-header">Find a Category</li>
<form>
<label></label>
<li><input class="search-query span10" type="text" name="suggestion" value="" id="suggestion" /></li>
</form>
</ul>
<div id="cats">
</div>
你知道吗视图.py你知道吗
def get_category_list(max_results=0, starts_with=''):
cat_list = []
if starts_with:
cat_list = Category.objects.filter(name__istartswith=starts_with)
if max_results > 0:
if len(cat_list) > max_results:
cat_list = cat_list[:max_results]
return cat_list
def suggest_category(request):
cat_list = []
starts_with = ''
if request.method == 'GET':
starts_with = request.GET['suggestion']
cat_list = get_category_list(8, starts_with)
return render(request, 'rango/cats.html', {'cat_list': cat_list})
你知道吗猫.html你知道吗
{% if cats %}
<ul class="nav nav-sidebar">
{% for c in cats %}
{% if c == act_act %} <li class="active"> {% else %} <li>{% endif %}
<a href="{% url 'category' c.slug %}">{{ c.name }}</a></li>
{% endfor %}
{% else %}
<li><strong>There are no category present.</strong></li>
</ul>
{% endif %}
js/兰戈-阿贾克斯.js你知道吗
$(document).ready(function(){
$('#likes').click(function(){
var catid;
catid = $(this).attr("data-catid");
$.get('/rango/like_category/', {category_id: catid}, function(data){
$('#like_count').html(data);
$('#likes').hide();
});
});
$('#suggestion').keyup(function(){
var query;
query = $(this).val();
$.get('/rango/suggest_category/', {suggestion: query}, function(data){
$('#cats').html(data);
});
});
});
有人能帮我一下吗?非常感谢!你知道吗
你输入了一个错别字,这是编码时首先要检查的,特别是使用django模板,因为未绑定的变量不会引起错误。你知道吗
cats.html
cats
应该是cat_list
。没有act_act
变量传递给模板。你知道吗我还建议您避免使用小变量名。使用
cats
而不是categories
而不是act_act
使用active
或更好的active_category
。用category
代替不可读的c
。这使代码更具可读性,不仅因为名称更长,而且因为将简练/晦涩的单词翻译成英语不需要认知负荷。你知道吗我使用复数名称,即使英语单词的复数形式只有一个
s
。这听起来可能容易出错和输入错误,但实际上没关系,例如给定变量token
(而不是tok
),我将tokens
命名为list
或set
的token
。你知道吗相关问题 更多 >
编程相关推荐