Django post edit is not working.

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?

What you really want to do is work through the official Django tutorial. You’ve got a lot of code in this small example that isn’t necessary or is suboptimal. There are a lot of things that Django will do for you when working with forms that you would otherwise need to do manually. If you really want to learn Django, you’ll want to learn about all of it.

1 Like

I just find the problem… save() I forgot to put ()…