Django ajax search responds with 404 status





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm trying to make a simple search using ajax in my django application.



I have the following model:



class Ingredient(models.Model):
name = models.CharField(max_length=200)


And here's my javascript:



function search_success(data){
$('#search_results').hmtl(data);
}

$(document).ready(function () {
$("#id_ingredient").on('change', function () {
update_unities();
});

$('#search_ing').on('keyup', function(){
$.ajax({
url: '/ajax/search/',
data: {
'search_text': $('#search_ing').val(),
},
dataType: 'html',
success: search_success,
});
});
});


In my urls.py, I added the following:



path('ajax/search', views.search_ingredients, name='search_ingredients')


And in my view, I defined the following function:



def search_ingredients(request):
if request.method == "POST":
search_text = request.POST['search_text']
else:
search_text = ''

ingredients = Ingredient.objects.filter(title__contains=search_text)
return render('recipe/search_ingredient.html', {'ingredients' : ingredients})


I'm getting a 404 error on my request, can someone help me out?



EDIT: Here's my folder structure.



.
├── admin.py
├── apps.py
├── forms.py
├── migrations
│   ├── omitted
├── models.py
├── static
│   └── css
│   └── recipe.css
├── templates
│   └── recipe
│   ├── base.html
│   ├── recipe_add_ingredient.html
│   ├── recipe_detail.html
│   ├── recipe_edit.html
│   ├── recipe_list.html
│   └── search_ingredient.html
├── tests.py
├── urls.py
└── views.py









share|improve this question

























  • You omitted the trailing slash in your path line.

    – Selcuk
    Nov 22 '18 at 23:21











  • @Selcuk you mean just doing path('ajax/search/', ...) ? Do you have any other idea? I added it and still getting the same error, I will edit my question with the folder structure.

    – Luc
    Nov 22 '18 at 23:25








  • 1





    Yes, that was my suggestion. I suggest you to enable DEBUG and visit the URL (http://localhost/ajax/search/) manually to see what is wrong.

    – Selcuk
    Nov 22 '18 at 23:28













  • Your terminal should show you the exact URL you are requesting. Very likely, you are just a folder up/down from where you should be.

    – WayBehind
    Nov 22 '18 at 23:29











  • @WayBehind Yea, I noticed I had two things wrong (updated on question) and when I access the url that's being requested I get a TemplateDoesNotExist and it points to this line in my views: return render('recipe/search_ingredients.html', {'ingredients' : ingredients}). You have any idea or anything that could help?

    – Luc
    Nov 22 '18 at 23:40


















0















I'm trying to make a simple search using ajax in my django application.



I have the following model:



class Ingredient(models.Model):
name = models.CharField(max_length=200)


And here's my javascript:



function search_success(data){
$('#search_results').hmtl(data);
}

$(document).ready(function () {
$("#id_ingredient").on('change', function () {
update_unities();
});

$('#search_ing').on('keyup', function(){
$.ajax({
url: '/ajax/search/',
data: {
'search_text': $('#search_ing').val(),
},
dataType: 'html',
success: search_success,
});
});
});


In my urls.py, I added the following:



path('ajax/search', views.search_ingredients, name='search_ingredients')


And in my view, I defined the following function:



def search_ingredients(request):
if request.method == "POST":
search_text = request.POST['search_text']
else:
search_text = ''

ingredients = Ingredient.objects.filter(title__contains=search_text)
return render('recipe/search_ingredient.html', {'ingredients' : ingredients})


I'm getting a 404 error on my request, can someone help me out?



EDIT: Here's my folder structure.



.
├── admin.py
├── apps.py
├── forms.py
├── migrations
│   ├── omitted
├── models.py
├── static
│   └── css
│   └── recipe.css
├── templates
│   └── recipe
│   ├── base.html
│   ├── recipe_add_ingredient.html
│   ├── recipe_detail.html
│   ├── recipe_edit.html
│   ├── recipe_list.html
│   └── search_ingredient.html
├── tests.py
├── urls.py
└── views.py









share|improve this question

























  • You omitted the trailing slash in your path line.

    – Selcuk
    Nov 22 '18 at 23:21











  • @Selcuk you mean just doing path('ajax/search/', ...) ? Do you have any other idea? I added it and still getting the same error, I will edit my question with the folder structure.

    – Luc
    Nov 22 '18 at 23:25








  • 1





    Yes, that was my suggestion. I suggest you to enable DEBUG and visit the URL (http://localhost/ajax/search/) manually to see what is wrong.

    – Selcuk
    Nov 22 '18 at 23:28













  • Your terminal should show you the exact URL you are requesting. Very likely, you are just a folder up/down from where you should be.

    – WayBehind
    Nov 22 '18 at 23:29











  • @WayBehind Yea, I noticed I had two things wrong (updated on question) and when I access the url that's being requested I get a TemplateDoesNotExist and it points to this line in my views: return render('recipe/search_ingredients.html', {'ingredients' : ingredients}). You have any idea or anything that could help?

    – Luc
    Nov 22 '18 at 23:40














0












0








0








I'm trying to make a simple search using ajax in my django application.



I have the following model:



class Ingredient(models.Model):
name = models.CharField(max_length=200)


And here's my javascript:



function search_success(data){
$('#search_results').hmtl(data);
}

$(document).ready(function () {
$("#id_ingredient").on('change', function () {
update_unities();
});

$('#search_ing').on('keyup', function(){
$.ajax({
url: '/ajax/search/',
data: {
'search_text': $('#search_ing').val(),
},
dataType: 'html',
success: search_success,
});
});
});


In my urls.py, I added the following:



path('ajax/search', views.search_ingredients, name='search_ingredients')


And in my view, I defined the following function:



def search_ingredients(request):
if request.method == "POST":
search_text = request.POST['search_text']
else:
search_text = ''

ingredients = Ingredient.objects.filter(title__contains=search_text)
return render('recipe/search_ingredient.html', {'ingredients' : ingredients})


I'm getting a 404 error on my request, can someone help me out?



EDIT: Here's my folder structure.



.
├── admin.py
├── apps.py
├── forms.py
├── migrations
│   ├── omitted
├── models.py
├── static
│   └── css
│   └── recipe.css
├── templates
│   └── recipe
│   ├── base.html
│   ├── recipe_add_ingredient.html
│   ├── recipe_detail.html
│   ├── recipe_edit.html
│   ├── recipe_list.html
│   └── search_ingredient.html
├── tests.py
├── urls.py
└── views.py









share|improve this question
















I'm trying to make a simple search using ajax in my django application.



I have the following model:



class Ingredient(models.Model):
name = models.CharField(max_length=200)


And here's my javascript:



function search_success(data){
$('#search_results').hmtl(data);
}

$(document).ready(function () {
$("#id_ingredient").on('change', function () {
update_unities();
});

$('#search_ing').on('keyup', function(){
$.ajax({
url: '/ajax/search/',
data: {
'search_text': $('#search_ing').val(),
},
dataType: 'html',
success: search_success,
});
});
});


In my urls.py, I added the following:



path('ajax/search', views.search_ingredients, name='search_ingredients')


And in my view, I defined the following function:



def search_ingredients(request):
if request.method == "POST":
search_text = request.POST['search_text']
else:
search_text = ''

ingredients = Ingredient.objects.filter(title__contains=search_text)
return render('recipe/search_ingredient.html', {'ingredients' : ingredients})


I'm getting a 404 error on my request, can someone help me out?



EDIT: Here's my folder structure.



.
├── admin.py
├── apps.py
├── forms.py
├── migrations
│   ├── omitted
├── models.py
├── static
│   └── css
│   └── recipe.css
├── templates
│   └── recipe
│   ├── base.html
│   ├── recipe_add_ingredient.html
│   ├── recipe_detail.html
│   ├── recipe_edit.html
│   ├── recipe_list.html
│   └── search_ingredient.html
├── tests.py
├── urls.py
└── views.py






jquery ajax django






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 23:41







Luc

















asked Nov 22 '18 at 23:16









LucLuc

489




489













  • You omitted the trailing slash in your path line.

    – Selcuk
    Nov 22 '18 at 23:21











  • @Selcuk you mean just doing path('ajax/search/', ...) ? Do you have any other idea? I added it and still getting the same error, I will edit my question with the folder structure.

    – Luc
    Nov 22 '18 at 23:25








  • 1





    Yes, that was my suggestion. I suggest you to enable DEBUG and visit the URL (http://localhost/ajax/search/) manually to see what is wrong.

    – Selcuk
    Nov 22 '18 at 23:28













  • Your terminal should show you the exact URL you are requesting. Very likely, you are just a folder up/down from where you should be.

    – WayBehind
    Nov 22 '18 at 23:29











  • @WayBehind Yea, I noticed I had two things wrong (updated on question) and when I access the url that's being requested I get a TemplateDoesNotExist and it points to this line in my views: return render('recipe/search_ingredients.html', {'ingredients' : ingredients}). You have any idea or anything that could help?

    – Luc
    Nov 22 '18 at 23:40



















  • You omitted the trailing slash in your path line.

    – Selcuk
    Nov 22 '18 at 23:21











  • @Selcuk you mean just doing path('ajax/search/', ...) ? Do you have any other idea? I added it and still getting the same error, I will edit my question with the folder structure.

    – Luc
    Nov 22 '18 at 23:25








  • 1





    Yes, that was my suggestion. I suggest you to enable DEBUG and visit the URL (http://localhost/ajax/search/) manually to see what is wrong.

    – Selcuk
    Nov 22 '18 at 23:28













  • Your terminal should show you the exact URL you are requesting. Very likely, you are just a folder up/down from where you should be.

    – WayBehind
    Nov 22 '18 at 23:29











  • @WayBehind Yea, I noticed I had two things wrong (updated on question) and when I access the url that's being requested I get a TemplateDoesNotExist and it points to this line in my views: return render('recipe/search_ingredients.html', {'ingredients' : ingredients}). You have any idea or anything that could help?

    – Luc
    Nov 22 '18 at 23:40

















You omitted the trailing slash in your path line.

– Selcuk
Nov 22 '18 at 23:21





You omitted the trailing slash in your path line.

– Selcuk
Nov 22 '18 at 23:21













@Selcuk you mean just doing path('ajax/search/', ...) ? Do you have any other idea? I added it and still getting the same error, I will edit my question with the folder structure.

– Luc
Nov 22 '18 at 23:25







@Selcuk you mean just doing path('ajax/search/', ...) ? Do you have any other idea? I added it and still getting the same error, I will edit my question with the folder structure.

– Luc
Nov 22 '18 at 23:25






1




1





Yes, that was my suggestion. I suggest you to enable DEBUG and visit the URL (http://localhost/ajax/search/) manually to see what is wrong.

– Selcuk
Nov 22 '18 at 23:28







Yes, that was my suggestion. I suggest you to enable DEBUG and visit the URL (http://localhost/ajax/search/) manually to see what is wrong.

– Selcuk
Nov 22 '18 at 23:28















Your terminal should show you the exact URL you are requesting. Very likely, you are just a folder up/down from where you should be.

– WayBehind
Nov 22 '18 at 23:29





Your terminal should show you the exact URL you are requesting. Very likely, you are just a folder up/down from where you should be.

– WayBehind
Nov 22 '18 at 23:29













@WayBehind Yea, I noticed I had two things wrong (updated on question) and when I access the url that's being requested I get a TemplateDoesNotExist and it points to this line in my views: return render('recipe/search_ingredients.html', {'ingredients' : ingredients}). You have any idea or anything that could help?

– Luc
Nov 22 '18 at 23:40





@WayBehind Yea, I noticed I had two things wrong (updated on question) and when I access the url that's being requested I get a TemplateDoesNotExist and it points to this line in my views: return render('recipe/search_ingredients.html', {'ingredients' : ingredients}). You have any idea or anything that could help?

– Luc
Nov 22 '18 at 23:40












1 Answer
1






active

oldest

votes


















0














fist of all:



change this:



path('ajax/search', views.search_ingredients, name='search_ingredients')


by this:



path('ajax/search/', views.search_ingredients, name='search_ingredients')


check the trailing slash at the end of the expression:



in the return statement of your view function, it should start with the request:



return render(request, 'recipe/search_ingredient.html', {'ingredients' : ingredients})


I hope it can help you






share|improve this answer
























  • Thanks for the answer @ensarman but unfortunately it didn't solve it. I'm still getting the same 404 error. Any other idea? I can share my github where I have the whole code if you wanna take a look.

    – Luc
    Nov 23 '18 at 10:28











  • ok let me see...

    – ensarman
    Nov 23 '18 at 16:43












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438985%2fdjango-ajax-search-responds-with-404-status%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














fist of all:



change this:



path('ajax/search', views.search_ingredients, name='search_ingredients')


by this:



path('ajax/search/', views.search_ingredients, name='search_ingredients')


check the trailing slash at the end of the expression:



in the return statement of your view function, it should start with the request:



return render(request, 'recipe/search_ingredient.html', {'ingredients' : ingredients})


I hope it can help you






share|improve this answer
























  • Thanks for the answer @ensarman but unfortunately it didn't solve it. I'm still getting the same 404 error. Any other idea? I can share my github where I have the whole code if you wanna take a look.

    – Luc
    Nov 23 '18 at 10:28











  • ok let me see...

    – ensarman
    Nov 23 '18 at 16:43
















0














fist of all:



change this:



path('ajax/search', views.search_ingredients, name='search_ingredients')


by this:



path('ajax/search/', views.search_ingredients, name='search_ingredients')


check the trailing slash at the end of the expression:



in the return statement of your view function, it should start with the request:



return render(request, 'recipe/search_ingredient.html', {'ingredients' : ingredients})


I hope it can help you






share|improve this answer
























  • Thanks for the answer @ensarman but unfortunately it didn't solve it. I'm still getting the same 404 error. Any other idea? I can share my github where I have the whole code if you wanna take a look.

    – Luc
    Nov 23 '18 at 10:28











  • ok let me see...

    – ensarman
    Nov 23 '18 at 16:43














0












0








0







fist of all:



change this:



path('ajax/search', views.search_ingredients, name='search_ingredients')


by this:



path('ajax/search/', views.search_ingredients, name='search_ingredients')


check the trailing slash at the end of the expression:



in the return statement of your view function, it should start with the request:



return render(request, 'recipe/search_ingredient.html', {'ingredients' : ingredients})


I hope it can help you






share|improve this answer













fist of all:



change this:



path('ajax/search', views.search_ingredients, name='search_ingredients')


by this:



path('ajax/search/', views.search_ingredients, name='search_ingredients')


check the trailing slash at the end of the expression:



in the return statement of your view function, it should start with the request:



return render(request, 'recipe/search_ingredient.html', {'ingredients' : ingredients})


I hope it can help you







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 6:03









ensarmanensarman

406




406













  • Thanks for the answer @ensarman but unfortunately it didn't solve it. I'm still getting the same 404 error. Any other idea? I can share my github where I have the whole code if you wanna take a look.

    – Luc
    Nov 23 '18 at 10:28











  • ok let me see...

    – ensarman
    Nov 23 '18 at 16:43



















  • Thanks for the answer @ensarman but unfortunately it didn't solve it. I'm still getting the same 404 error. Any other idea? I can share my github where I have the whole code if you wanna take a look.

    – Luc
    Nov 23 '18 at 10:28











  • ok let me see...

    – ensarman
    Nov 23 '18 at 16:43

















Thanks for the answer @ensarman but unfortunately it didn't solve it. I'm still getting the same 404 error. Any other idea? I can share my github where I have the whole code if you wanna take a look.

– Luc
Nov 23 '18 at 10:28





Thanks for the answer @ensarman but unfortunately it didn't solve it. I'm still getting the same 404 error. Any other idea? I can share my github where I have the whole code if you wanna take a look.

– Luc
Nov 23 '18 at 10:28













ok let me see...

– ensarman
Nov 23 '18 at 16:43





ok let me see...

– ensarman
Nov 23 '18 at 16:43




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438985%2fdjango-ajax-search-responds-with-404-status%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?