Django ajax search responds with 404 status
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
|
show 4 more comments
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
You omitted the trailing slash in yourpath
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
|
show 4 more comments
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
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
jquery ajax django
edited Nov 22 '18 at 23:41
asked Nov 22 '18 at 23:16
Luc
338
338
You omitted the trailing slash in yourpath
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
|
show 4 more comments
You omitted the trailing slash in yourpath
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
|
show 4 more comments
1 Answer
1
active
oldest
votes
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
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
answered Nov 23 '18 at 6:03
ensarman
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
add a comment |
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
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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