edche
edche

Reputation: 678

Django how to create a filled update object form?

I want to create an update form. When a user enters this page the form should be filled with information so that the user can edit what they want to fix. I try to use instance in views but didn't work. The fields are still empty. How can I do it?

views.py

def setup_settings(request):
    user = request.user
    data = get_object_or_404(DashboardData, user=user)
    # print(data) --> DashboardData object (45)
    form = SetupForm(request.POST or None, instance=data)
    if request.method == 'POST':
        if form.is_valid():
            form.save()
    else:
        form = SetupForm()

  context = {
    'form': form,
  }
   return render(request, 'update_setup.html', context)

Upvotes: 0

Views: 83

Answers (1)

Faizan AlHassan
Faizan AlHassan

Reputation: 429

Basically, in your else block, you have overwritten form with the empty object SetupForm(). When the user will visit the page, it will hit a GET request and your else block will make your form empty, try again after removing it.

Upvotes: 1

Related Questions