Django request.method == 'POST' returning false











up vote
0
down vote

favorite












Currently working on my first django web application so still quite rusty.
Trying to create a page that allows a lecturer to create a new class, the class is then added to the lecturers list of classes.



As far as I can tell, from following a tutorial, the request.method in my views.py should return a POST, but is currently returning GET.



Because of this, the new class created is never saved to the database.



here is my views.py



def create_class(request):
if request.user.is_lecturer:

print(" ")
print("user is lecturer")
form = classForm()
print(request.method)
if request.method == 'POST':
print("")
print("request method equals post")
form = classForm(request.POST)
if form.is_valid():

lecturer = LecturerProfile.objects.get(lecturer=request.user)
subject = Class.objects.get_or_create(class_name=form.cleaned_data["class_name"],
class_description=form.cleaned_data["class_description"],
lecturer=lecturer)
LecturerProfile.Classes.add(subject)
form.save(commit=True)
return index(request)
else:
print("")
print(form.errors)

else:
print("")
print("request method fail")
else:
return HttpResponse("You are not allowed here")

return render(request, 'student_feedback_app/create_class.html', {'form':form})


forms.py



class classForm(forms.ModelForm):
subject = forms.CharField(max_length=40, help_text="Class Name", required=False)
class_description = forms.CharField(max_length=200, required=False, help_text="Class Description")
slug = forms.CharField(widget=forms.HiddenInput(), required=False)

class Meta:
model = Class
fields = ('subject', 'class_description',)


my create_class.html



<!DOCTYPE html>
<html>

<head>
Create a new class

<h1> Create a new class</h1>
</head>

<body>
<div>
<form id="" method="post" action="/lecturer/classes/">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
<br>
{{ field.errors }}
{{ field.help_text }}
{{ field }}
{% endfor %}
<br>
<input type="submit" name="submit" value="Create Class" /> </form>

</div>
</body>

</html>


The errors that I get out at the terminal are




user is lecturer GET



request method fail



[11/Nov/2018 16:27:24] "GET /lecturer/create-class/ HTTP/1.1" 200 923



Not Found: /lecturer/create-class/js/plugins/jqBootstrapValidation.js



[11/Nov/2018 16:27:24] "GET /lecturer/create-class/js/plugins/jqBootstrapValidation.js HTTP/1.1" 404 4369




From the errors it would seem like it must be something to do with Bootstrap that the error is coming from, although i haven't used this for anything.
I even took out the base_template.html and wrote the create_class.html normally incase something from it was using javascript and bootstrap, but got the same output.
Any help is very appreciated










share|improve this question
























  • Are you sure with your form? client side? check with the network debugger if you really post the data.
    – Bast
    Nov 11 at 17:00










  • I'm sorry but im afraid I'll need a little bit more instructions on how to check that. Do you mean by using the admin interface? or using an actual debugging tool like Pdb
    – Adam Christie
    Nov 11 at 17:12










  • No, it won't be POST until you submit the form. The first request, to display the form, will be GET.
    – Daniel Roseman
    Nov 11 at 17:15










  • @DanielRoseman Ah yeah i've realised that now, this however still doesnt help, as once the form is submitted im stuck in the same position where the data isnt being saved. Also once I hit submit, surely nothing will happen as I've already dropped out of the if, does it not have to equal POST to begin with?
    – Adam Christie
    Nov 11 at 17:17












  • Well, there are several errors here, but the most obvious is that if request.POST.get("create_class") will never be true because you don't have anything in your form called "create_class". What's the point of that condition? And I don't understand what you mean by "I've already dropped out of the if".
    – Daniel Roseman
    Nov 11 at 17:20

















up vote
0
down vote

favorite












Currently working on my first django web application so still quite rusty.
Trying to create a page that allows a lecturer to create a new class, the class is then added to the lecturers list of classes.



As far as I can tell, from following a tutorial, the request.method in my views.py should return a POST, but is currently returning GET.



Because of this, the new class created is never saved to the database.



here is my views.py



def create_class(request):
if request.user.is_lecturer:

print(" ")
print("user is lecturer")
form = classForm()
print(request.method)
if request.method == 'POST':
print("")
print("request method equals post")
form = classForm(request.POST)
if form.is_valid():

lecturer = LecturerProfile.objects.get(lecturer=request.user)
subject = Class.objects.get_or_create(class_name=form.cleaned_data["class_name"],
class_description=form.cleaned_data["class_description"],
lecturer=lecturer)
LecturerProfile.Classes.add(subject)
form.save(commit=True)
return index(request)
else:
print("")
print(form.errors)

else:
print("")
print("request method fail")
else:
return HttpResponse("You are not allowed here")

return render(request, 'student_feedback_app/create_class.html', {'form':form})


forms.py



class classForm(forms.ModelForm):
subject = forms.CharField(max_length=40, help_text="Class Name", required=False)
class_description = forms.CharField(max_length=200, required=False, help_text="Class Description")
slug = forms.CharField(widget=forms.HiddenInput(), required=False)

class Meta:
model = Class
fields = ('subject', 'class_description',)


my create_class.html



<!DOCTYPE html>
<html>

<head>
Create a new class

<h1> Create a new class</h1>
</head>

<body>
<div>
<form id="" method="post" action="/lecturer/classes/">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
<br>
{{ field.errors }}
{{ field.help_text }}
{{ field }}
{% endfor %}
<br>
<input type="submit" name="submit" value="Create Class" /> </form>

</div>
</body>

</html>


The errors that I get out at the terminal are




user is lecturer GET



request method fail



[11/Nov/2018 16:27:24] "GET /lecturer/create-class/ HTTP/1.1" 200 923



Not Found: /lecturer/create-class/js/plugins/jqBootstrapValidation.js



[11/Nov/2018 16:27:24] "GET /lecturer/create-class/js/plugins/jqBootstrapValidation.js HTTP/1.1" 404 4369




From the errors it would seem like it must be something to do with Bootstrap that the error is coming from, although i haven't used this for anything.
I even took out the base_template.html and wrote the create_class.html normally incase something from it was using javascript and bootstrap, but got the same output.
Any help is very appreciated










share|improve this question
























  • Are you sure with your form? client side? check with the network debugger if you really post the data.
    – Bast
    Nov 11 at 17:00










  • I'm sorry but im afraid I'll need a little bit more instructions on how to check that. Do you mean by using the admin interface? or using an actual debugging tool like Pdb
    – Adam Christie
    Nov 11 at 17:12










  • No, it won't be POST until you submit the form. The first request, to display the form, will be GET.
    – Daniel Roseman
    Nov 11 at 17:15










  • @DanielRoseman Ah yeah i've realised that now, this however still doesnt help, as once the form is submitted im stuck in the same position where the data isnt being saved. Also once I hit submit, surely nothing will happen as I've already dropped out of the if, does it not have to equal POST to begin with?
    – Adam Christie
    Nov 11 at 17:17












  • Well, there are several errors here, but the most obvious is that if request.POST.get("create_class") will never be true because you don't have anything in your form called "create_class". What's the point of that condition? And I don't understand what you mean by "I've already dropped out of the if".
    – Daniel Roseman
    Nov 11 at 17:20















up vote
0
down vote

favorite









up vote
0
down vote

favorite











Currently working on my first django web application so still quite rusty.
Trying to create a page that allows a lecturer to create a new class, the class is then added to the lecturers list of classes.



As far as I can tell, from following a tutorial, the request.method in my views.py should return a POST, but is currently returning GET.



Because of this, the new class created is never saved to the database.



here is my views.py



def create_class(request):
if request.user.is_lecturer:

print(" ")
print("user is lecturer")
form = classForm()
print(request.method)
if request.method == 'POST':
print("")
print("request method equals post")
form = classForm(request.POST)
if form.is_valid():

lecturer = LecturerProfile.objects.get(lecturer=request.user)
subject = Class.objects.get_or_create(class_name=form.cleaned_data["class_name"],
class_description=form.cleaned_data["class_description"],
lecturer=lecturer)
LecturerProfile.Classes.add(subject)
form.save(commit=True)
return index(request)
else:
print("")
print(form.errors)

else:
print("")
print("request method fail")
else:
return HttpResponse("You are not allowed here")

return render(request, 'student_feedback_app/create_class.html', {'form':form})


forms.py



class classForm(forms.ModelForm):
subject = forms.CharField(max_length=40, help_text="Class Name", required=False)
class_description = forms.CharField(max_length=200, required=False, help_text="Class Description")
slug = forms.CharField(widget=forms.HiddenInput(), required=False)

class Meta:
model = Class
fields = ('subject', 'class_description',)


my create_class.html



<!DOCTYPE html>
<html>

<head>
Create a new class

<h1> Create a new class</h1>
</head>

<body>
<div>
<form id="" method="post" action="/lecturer/classes/">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
<br>
{{ field.errors }}
{{ field.help_text }}
{{ field }}
{% endfor %}
<br>
<input type="submit" name="submit" value="Create Class" /> </form>

</div>
</body>

</html>


The errors that I get out at the terminal are




user is lecturer GET



request method fail



[11/Nov/2018 16:27:24] "GET /lecturer/create-class/ HTTP/1.1" 200 923



Not Found: /lecturer/create-class/js/plugins/jqBootstrapValidation.js



[11/Nov/2018 16:27:24] "GET /lecturer/create-class/js/plugins/jqBootstrapValidation.js HTTP/1.1" 404 4369




From the errors it would seem like it must be something to do with Bootstrap that the error is coming from, although i haven't used this for anything.
I even took out the base_template.html and wrote the create_class.html normally incase something from it was using javascript and bootstrap, but got the same output.
Any help is very appreciated










share|improve this question















Currently working on my first django web application so still quite rusty.
Trying to create a page that allows a lecturer to create a new class, the class is then added to the lecturers list of classes.



As far as I can tell, from following a tutorial, the request.method in my views.py should return a POST, but is currently returning GET.



Because of this, the new class created is never saved to the database.



here is my views.py



def create_class(request):
if request.user.is_lecturer:

print(" ")
print("user is lecturer")
form = classForm()
print(request.method)
if request.method == 'POST':
print("")
print("request method equals post")
form = classForm(request.POST)
if form.is_valid():

lecturer = LecturerProfile.objects.get(lecturer=request.user)
subject = Class.objects.get_or_create(class_name=form.cleaned_data["class_name"],
class_description=form.cleaned_data["class_description"],
lecturer=lecturer)
LecturerProfile.Classes.add(subject)
form.save(commit=True)
return index(request)
else:
print("")
print(form.errors)

else:
print("")
print("request method fail")
else:
return HttpResponse("You are not allowed here")

return render(request, 'student_feedback_app/create_class.html', {'form':form})


forms.py



class classForm(forms.ModelForm):
subject = forms.CharField(max_length=40, help_text="Class Name", required=False)
class_description = forms.CharField(max_length=200, required=False, help_text="Class Description")
slug = forms.CharField(widget=forms.HiddenInput(), required=False)

class Meta:
model = Class
fields = ('subject', 'class_description',)


my create_class.html



<!DOCTYPE html>
<html>

<head>
Create a new class

<h1> Create a new class</h1>
</head>

<body>
<div>
<form id="" method="post" action="/lecturer/classes/">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
<br>
{{ field.errors }}
{{ field.help_text }}
{{ field }}
{% endfor %}
<br>
<input type="submit" name="submit" value="Create Class" /> </form>

</div>
</body>

</html>


The errors that I get out at the terminal are




user is lecturer GET



request method fail



[11/Nov/2018 16:27:24] "GET /lecturer/create-class/ HTTP/1.1" 200 923



Not Found: /lecturer/create-class/js/plugins/jqBootstrapValidation.js



[11/Nov/2018 16:27:24] "GET /lecturer/create-class/js/plugins/jqBootstrapValidation.js HTTP/1.1" 404 4369




From the errors it would seem like it must be something to do with Bootstrap that the error is coming from, although i haven't used this for anything.
I even took out the base_template.html and wrote the create_class.html normally incase something from it was using javascript and bootstrap, but got the same output.
Any help is very appreciated







javascript django web-applications django-forms django-views






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 17:32

























asked Nov 11 at 16:40









Adam Christie

43




43












  • Are you sure with your form? client side? check with the network debugger if you really post the data.
    – Bast
    Nov 11 at 17:00










  • I'm sorry but im afraid I'll need a little bit more instructions on how to check that. Do you mean by using the admin interface? or using an actual debugging tool like Pdb
    – Adam Christie
    Nov 11 at 17:12










  • No, it won't be POST until you submit the form. The first request, to display the form, will be GET.
    – Daniel Roseman
    Nov 11 at 17:15










  • @DanielRoseman Ah yeah i've realised that now, this however still doesnt help, as once the form is submitted im stuck in the same position where the data isnt being saved. Also once I hit submit, surely nothing will happen as I've already dropped out of the if, does it not have to equal POST to begin with?
    – Adam Christie
    Nov 11 at 17:17












  • Well, there are several errors here, but the most obvious is that if request.POST.get("create_class") will never be true because you don't have anything in your form called "create_class". What's the point of that condition? And I don't understand what you mean by "I've already dropped out of the if".
    – Daniel Roseman
    Nov 11 at 17:20




















  • Are you sure with your form? client side? check with the network debugger if you really post the data.
    – Bast
    Nov 11 at 17:00










  • I'm sorry but im afraid I'll need a little bit more instructions on how to check that. Do you mean by using the admin interface? or using an actual debugging tool like Pdb
    – Adam Christie
    Nov 11 at 17:12










  • No, it won't be POST until you submit the form. The first request, to display the form, will be GET.
    – Daniel Roseman
    Nov 11 at 17:15










  • @DanielRoseman Ah yeah i've realised that now, this however still doesnt help, as once the form is submitted im stuck in the same position where the data isnt being saved. Also once I hit submit, surely nothing will happen as I've already dropped out of the if, does it not have to equal POST to begin with?
    – Adam Christie
    Nov 11 at 17:17












  • Well, there are several errors here, but the most obvious is that if request.POST.get("create_class") will never be true because you don't have anything in your form called "create_class". What's the point of that condition? And I don't understand what you mean by "I've already dropped out of the if".
    – Daniel Roseman
    Nov 11 at 17:20


















Are you sure with your form? client side? check with the network debugger if you really post the data.
– Bast
Nov 11 at 17:00




Are you sure with your form? client side? check with the network debugger if you really post the data.
– Bast
Nov 11 at 17:00












I'm sorry but im afraid I'll need a little bit more instructions on how to check that. Do you mean by using the admin interface? or using an actual debugging tool like Pdb
– Adam Christie
Nov 11 at 17:12




I'm sorry but im afraid I'll need a little bit more instructions on how to check that. Do you mean by using the admin interface? or using an actual debugging tool like Pdb
– Adam Christie
Nov 11 at 17:12












No, it won't be POST until you submit the form. The first request, to display the form, will be GET.
– Daniel Roseman
Nov 11 at 17:15




No, it won't be POST until you submit the form. The first request, to display the form, will be GET.
– Daniel Roseman
Nov 11 at 17:15












@DanielRoseman Ah yeah i've realised that now, this however still doesnt help, as once the form is submitted im stuck in the same position where the data isnt being saved. Also once I hit submit, surely nothing will happen as I've already dropped out of the if, does it not have to equal POST to begin with?
– Adam Christie
Nov 11 at 17:17






@DanielRoseman Ah yeah i've realised that now, this however still doesnt help, as once the form is submitted im stuck in the same position where the data isnt being saved. Also once I hit submit, surely nothing will happen as I've already dropped out of the if, does it not have to equal POST to begin with?
– Adam Christie
Nov 11 at 17:17














Well, there are several errors here, but the most obvious is that if request.POST.get("create_class") will never be true because you don't have anything in your form called "create_class". What's the point of that condition? And I don't understand what you mean by "I've already dropped out of the if".
– Daniel Roseman
Nov 11 at 17:20






Well, there are several errors here, but the most obvious is that if request.POST.get("create_class") will never be true because you don't have anything in your form called "create_class". What's the point of that condition? And I don't understand what you mean by "I've already dropped out of the if".
– Daniel Roseman
Nov 11 at 17:20



















active

oldest

votes











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',
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%2f53250889%2fdjango-request-method-post-returning-false%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.





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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250889%2fdjango-request-method-post-returning-false%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

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)