Reputation: 428
When I make a post in a update view this redirect to another url but not to that I want. I have an application called project
.
This is my project.urls.py
:
urlpatterns = [
...
path('edit/<int:pk>', ProjectUpdateView.as_view(), name='project_update'),
path('detail/<int:pk>', ProjectDetailView.as_view(), name='project_detail'),
...
]
And this is my views.py
:
class ProjectUpdateView(UpdateView):
model = Project
...
def get_success_url(self):
return reverse_lazy('project_detail', kwargs={'pk': self.object.pk})
And my models.py
:
class Project(models.Model):
name = models.CharField(max_length=100)
...
def get_absolute_url(self):
return reverse('project_detail', kwargs={'pk': self.pk})
When I make a post to this url /edit/5
this redirects me to /edit
and this page return me a 404 error, of course because this url doesn't exist.
how can i fix this? I'm using django 2.0
Update
this is my template project_edit.html
:
<form method='post'>
{% csrf_token %}
{{ form.as_p }}
<button type='submit' class='btn'>Aceoptar</button>
</form>
Upvotes: 2
Views: 2725
Reputation: 309089
I don't think this has anything to do with your get_success_url
or get_absolute_url` methods.
It sounds like your form action is:
<form method="post" action=".">
Since you are posting from /edit/5
(without a trailing slash), this means that you are posting to /edit/
.
You could change it to:
<form method="post" action="">
or reverse the url
<form method="post" action="{% url 'project_update' project.pk %}">
Upvotes: 3