This may possibly be a duplicate of this answer. Currently, i'm working on updating an object using the same model form that is used to create the object.
my views.py looks like: (as from the answer):
def newpost(request):
form = PostForm(request.POST)
if request.method == "POST":
if form.is_valid():
obj = form.save(commit=False)
obj.save()
return redirect('newpost')
return render(request, 'console/newpost.html', {'form':form})
def editpost(request, pk):
obj = Post.objects.get(id=pk)
form = PostForm(instance=obj)
if request.method == "POST":
if form.is_valid():
obj = form.save(commit=False)
obj.save()
return redirect('editpost')
return render(request, 'console/editpost.html', {'form':form})
And my html form in editpost looks like:
<form method="POST" action="{% url 'newpost' %}">
{% csrf_token %}
{{form.as_p}}
<button type="submit"> Submit</button>
</form>
and my urls.py looks like:
path('console/post/', c_views.newpost, name='newpost'),
path('console/post/<int:pk>/', c_views.editpost, name='editpost'),
And the above codes works perfectly fine, but creates a new instance, with the data of the object taken from pk.
I added a obj.delete() code like this:
def editpost(request, pk):
obj = Post.objects.get(id=pk)
form = PostForm(instance=obj)
obj.delete()
if request.method == "POST":
if form.is_valid():
obj = form.save(commit=False)
obj.save()
return redirect('editpost')
return render(request, 'console/editpost.html', {'form':form})
This code gives me the exact thing i wanted, but i know it's not a good practice. My question here is, is this a correct way or am i lagging somewhere.
I know the action in my editpost html should not be {% url 'newpost' %}, but if i use {% url 'editpost' %} i don't know how to pass the pk value inside the url tag. Can anyone suggest me the correct way?