0
我想向我的表單添加自動填充字段。但我不能工作。我已經嘗試過幾乎所有的教程。 請求發送正常,我得到一個200響應。在「開發人員工具」窗格中,單擊請求時,在「響應」選項卡上,我會看到整個HTML文件。不應該有一個JSON格式化的東西.. ?? 下面的代碼:Django JQuery自動完成
models.py
class Signers(models.Model):
uid = models.IntegerField(primary_key=True)
firstName = models.CharField(max_length=255)
lastName = models.CharField(max_length=255)
views.py
def get_signers(request):
if request.is_ajax():
q = request.GET.get('term', '')
signers = Signers.objects.filter(uid__icontains=q)[:20]
results = []
for s in signers:
signer_json = {}
signer_json['id'] = s.uid
signer_json['label'] = s.uid
signer_json['value'] = s.uid
results.append(signer_json)
data = json.dumps(results)
print data
else:
data = 'fail'
mimetype = 'application/json'
return HttpResponse(data,mimetype)
urls.py
url(r'^get_signers/', views.get_signers, name='get_signers'),
praxis.html(我的模板)
<script>
$(function() {
$("#id_signerIds").autocomplete({
source: "get_signers/",
minLength: 2,
});
});
</script>
<script>
$(document).ready(function() {
$('#id_submissionTimestamp').hide();
$('#id_issueDate').hide();
});
</script>
{% endblock page_head %}
{% block content %}
<form method="POST" class="post-form">{% csrf_token %}
<!--some other fields here... -->
<div class="ui-widget">
<label for="id_signerIds">Signers: </label>
<input id="id_signerIds">
</div>
<button type="submit" class="save btn btn-default">Save</button>
</form>
{% endblock %}
_「的jQuery的,但我不能噸至工作「_什麼不工作? –
@PacoH。當我輸入一些數字時,沒有結果顯示在輸入字段下面,就像我看到的 – Danae
一樣。試試[JsonResponse](https://docs.djangoproject.com/en/1.11/ref/request-response/#jsonresponse-objects)。如果它不起作用,請將請求的響應包含到'get_signers /' –