Reputation: 7
Im doing one udemy course of django, but the course works with django 1.1 or something like that, Im tryng to do it with django 2 and i have no problem with it in general, BUT when i try to send a post with author, tittle and text, the app redirect to 404 error.
the view.py code to create the post
class CreatePostView(LoginRequiredMixin, CreateView):
login_url = '/login/'
redirect_field_name = 'blog/post_detail.html'
form_class = PostForm
model = Post
the url.py on the blog app folder
from django.urls import path, re_path, include
from blog import views
urlpatterns = [
re_path(r'^$', views.PostListView.as_view(), name='post_list'),
re_path(r'^about/$', views.AboutView.as_view(), name='about'),
re_path(r'^post/(?P<pk>\d+)$',views.PostDetailView.as_view(),name='post_detail'),
re_path(r'^post/new/$', views.CreatePostView.as_view(),name='post_new'),
re_path(r'^post/(?P<pk>\d+)/edit/$',views.PostUpdateView.as_view(),name='post_edit'),
re_path(r'^post/(?P<pk>\d+)/remove/$',views.PostDeleteView.as_view(),name='post_remove'),
re_path(r'^drafts/$',views.DraftListView.as_view(),name='post_draft_list'),
re_path(r'^post/(?P<pk>\d+)/comment/$',views.add_comment_to_post,name='add_comment_to_post'),
re_path(r'^comment/(?P<pk>\d+)/approve/$',views.comment_approve,name='comment_approve'),
re_path(r'^comment/(?P<pk>\d+)/remove/$',views.comment_remove,name='comment_remove'),
re_path(r'^post/(?P<pk>\d+)/publish/$',views.post_publish,name='post_publish'),
]
the form.py
class PostForm(forms.ModelForm):
class Meta():
model = Post
fields = ('author', 'title', 'text')
widgets = {
'title':forms.TextInput(attrs={'class':'textinputclass'}),
'text':forms.Textarea(attrs={'class':'editable medium-editor-textarea postcontent'})
}
the html
<form action="POST" class="post-form">
{% csrf_token %}
{{ form.as_p }}
<button class="save btn btn-default" type="submit">Guardar</button>
</form>
the error: django debug page pointing the error
Upvotes: 0
Views: 627
Reputation: 2854
I think instead of writing action="POST"
<form action="POST" class="post-form">
{% csrf_token %}
{{ form.as_p }}
<button class="save btn btn-default" type="submit">Guardar</button>
</form>
You should write method="POST"
<form method="POST" class="post-form">
{% csrf_token %}
{{ form.as_p }}
<button class="save btn btn-default" type="submit">Guardar</button>
</form>
Upvotes: 0
Reputation: 24922
Your current form is incorrect.
<form action="POST" class="post-form">
{% csrf_token %}
{{ form.as_p }}
<button class="save btn btn-default" type="submit">Guardar</button>
</form>
As per the documentation,
Action
The action attribute specifies where to send the form-data when a form is submitted.
Method
The HTTP method that the browser uses to submit the form. Possible values are: post: Corresponds to the HTTP POST method ; form data are included in the body of the form and sent to the server. get: Corresponds to the HTTP GET method; form data are appended to the action attribute URI with a '?' as separator, and the resulting URI is sent to the server. Use this method when the form has no side-effects and contains only ASCII characters.
But that is not the case here. Since you have not mentioned any method attribute value, form will take GET
method as default value(That is why the data is getting appended with the URL during form submission(In your screenshot)), And your form is trying to POST the data into the url named POST
which does not exists(Hence getting the 404
Error)
Since you are trying to POST the data, you should set the method as POST
and set action as {% url 'post_new' %}
So your final form would be something like this.
<form action="{% url 'post_new' %}" class="post-form" method="POST">
{% csrf_token %}
{{ form.as_p }}
<button class="save btn btn-default" type="submit">Guardar</button>
</form>
Upvotes: 3