Reputation: 63
I have problems with the autocomplete in Django, Django recognize the autocomplete but it gets a 404 error when I try it
this is the part of the template I'm using the autocomplete
<table>
<td class="input-group mb-3"><label for="
{{TeacherForm.teacher.id_for_label}}"> Teacher(es):
<td> <div class="ui-widget">
{{TeacherForm.asesores}}</td>
</div>
</table>
This is my model:
class Teacher(models.Model):
firstname=models.CharField(max_length=50)
middlename=models.CharField(max_length=30)
lastaname=models.CharField(max_length=30)
This is my form:
class TeacherForm(forms.Form):
teacher=forms.CharField(widget=forms.Textarea)
This is the script I'm using:
<script>
$(function() {
$("#id_teacher").autocomplete({
source: "/api/get_teacher/",
select: function (event, ui) { //item selected
AutoCompleteSelectHandler(event, ui)
},
minLength: 2,
});
});
function AutoCompleteSelectHandler(event, ui)
{
var selectedObj = ui.item;
}
</script>
This is my url.py:
urlpatterns = [
url(r'^api/get_teacher/', views.get_teacher, name='get_teacher'),
]
And in my view.py I have:
def get_teacher(request):
if request.is_ajax():
q = request.GET.get('term', '')
asesor = Asesor.objects.filter(lastname=q)
results = []
for professor in teacher:
teacher_json = {}
teacher_json = professor.lastname+ " "+professor.firstname+"
"+professor.middlename
results.append(teacher_json)
data = json.dumps(results)
else:
data = 'fail'
mimetype = 'application/json'
return HttpResponse(data, mimetype)
In my base_layout I'm calling:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script>
<!-- jQuery UI !-->
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
When I try it, i get this error
GET http://localhost:8000/api/get_teacher/?term=Guerrer 404 (Not Found)
How can I solve the error
Upvotes: 1
Views: 453
Reputation: 8525
You call Search.urls
from the root urls
, with the following
url(r'^inicio/',include('Search.urls')),
In that case, your url is not /api/get_teacher/
, but instead a concatenation of the root inicio/
+ api/get_teacher/
. The correct url is:
/inicio/api/get_teacher/
`
Upvotes: 1