Hi! I use to code with flash and recently change to Django. I just start Django and having trouble with editing post. This is the code below.
post_form.html
{% extends 'blog/base.html' %}
{% block content %}
<form action="/blog/{{ blog_post.pk }}/update", method="POST">
{% csrf_token %}
<input type='text' name='title' value='{{ blog_post.title }}'/>
<input type='text' class='textinput' name='content' value='{{ blog_post.content }}'/>
<input type="submit" value="Submit"/>
</form>
{% endblock %}
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', index),
path('blog/<pk>', post_detail),
path('blog/<pk>/edit', postEdit),
path('blog/<pk>/update', postUpdate)
]
veiws.py
def postEdit(request, pk):
blog_post = Post.objects.get(id=pk)
return render(request, 'blog/post_form.html', {'blog_post':blog_post})
def postUpdate(request, pk):
if request.method == 'POST':
blog_post = Post.objects.get(id=pk)
blog_post.title = request.POST.get('title')
blog_post.content = request.POST.get('content')
blog_post.save
return redirect(f'/blog/{pk}')
When I edit the post and press submit, it redirect to the post detail page but the post isn’t changed. What did I do wrong?