Answer the question
In order to leave comments, you need to log in
Why is the post update link not working in Django?
Hello, the url link to the post update page does not work, and the page itself works fine.
Reverse for 'edit' with keyword arguments '{'pk': ''} - error like this
views.py
@login_required
def post_edit(request, pk):
post = Post.objects.get(id=pk)
form = PostEditForm(instance=post)
if request.method == 'POST':
form = PostEditForm(data=request.POST, files=request.FILES, instance=post)
if form.is_valid():
post = form.save(commit=False)
post.user = request.user
post.save()
messages.success(request, 'Post updated successfully')
return render(request, 'posts/post/edit.html', )
else:
messages.error(request, 'Error updating your post')
else:
form = PostEditForm(instance=post)
return render(request, 'posts/post/edit.html', {'form': form, 'instance': post})
urlpatterns = [
path('create/', views.post_create, name='create'),
path('edit/<int:pk>/', views.post_edit, name='edit'),
]
{% extends "base.html" %}
{% block title %}Good job{% endblock %}
{% block content %}
<h1> Hi, {{ user.username }}!</h1>
<p>Your post has been successfully add to platform. </p>
You can <a href="{% url "posts:edit" pk=post.id %}">edit your post</a>
{% endblock %}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question