Answer the question
In order to leave comments, you need to log in
How to get the id of the created record in the view that creates this record?
I don’t know if I formulated the question correctly, but the essence is this: after the form is validated, it is naturally saved. After saving, I want to send the user to view these saved data. To make a redirect to a post, you need to know the id of that post. So the question is, how to get this id? There is no cleaned_data id. Where to look for it?
views.py
def new_entry(request):
if request.method == "POST":
entry_form = EntryForm(request.POST or None)
if entry_form.is_valid():
entry_f.save()
messages.success(request, 'Запись в базе данных успешно создана!')
return redirect('current_entry', entry_id=???)
else:
entry_form = EntryForm()
context = {
'entry_form': entry_form,
}
return render(request, 'add_and_edit.html', context)
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