0

I am trying to process a post form, but I can't manage. I have prepared the form, gave the action link, set the method to post, but when I hit the submit button, nothing happens, not even an error page isn't shown by django despite my debug option is True.

Here is the form code in my template file:

<form method="post" action="{% url 'articles:stepprocess' 0 %}"> {% csrf_token %} <p><label for="title">Title:</label> <input name="title" id="title" type="text"/></p> <p> <label for="dif">Difficulty</label> <select name="dif" id="dif"> {% if difficulties %} {% for difficulty in difficulties %} <option value="{{ difficulty.pk }}">{{ difficulty }}</option> {% endfor %} {% endif %} </select> </p> <p><label for="article">Content:</label> <textarea cols="37" rows="11" name="article" id="article"></textarea></p> <p><input name="send" style="margin-left: 150px;" class="formbutton" value="Send" type="submit"/></p> </form> 

My urls.py file:

from django.conf.urls import url from .views import * urlpatterns = [ url(r'^$', Index.as_view(), name="index"), url(r'^new/(?P<step>[0-9]+)/$', NewArticle.as_view(), name="newarticle_continue"), url(r'^new/', NewArticle.as_view(), name="newarticle"), url(r'^new/process/(?P<step>[0-9]+)/$', FormManager.as_view(), name='stepprocess') #url(r'^show/(?P<article>[0-9]+)/$'), ] 

And lastly, my views.py file:

#required imports... class FormManager(View): def post(self, request, *args, **kwargs): return HttpResponse(kwargs['step']) 

When I hit the submit button, it gives me HTTP 405 error. I can see this error in python console but nothing shows in browser. Just blank white screen.

This was actually to test if view works properly. My final purpose is that I would like to access the post variables and redirect the page. However, HttpResponseRedirect also doesn't work. How can I fix this?

1 Answer 1

2

HTTP 405 error means "method not allowed"

In your case you are POSTing... the view looks like it should accept POST requests though.

The problem is your urls.py is wrong, the url you POSTed to is intercepted by the NewArticle view instead, which I guess only accepts GETs.

from django.conf.urls import url from .views import * urlpatterns = [ url(r'^$', Index.as_view(), name="index"), url(r'^new/process/(?P<step>[0-9]+)/$', FormManager.as_view(), name='stepprocess') url(r'^new/(?P<step>[0-9]+)/$', NewArticle.as_view(), name="newarticle_continue"), url(r'^new/', NewArticle.as_view(), name="newarticle"), #url(r'^show/(?P<article>[0-9]+)/$'), ] 

Django looks at the urls in the urlconf in the order they are defined and will dispatch your request to the first one that matches.

Above I have modified the order, alternatively you could just add the $ to end of url:

url(r'^new/$', NewArticle.as_view(), name="newarticle"), 

this will prevent it matching any new/* url paths.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.