Home > Back-end >  (Django) How to keep input values after form submit
(Django) How to keep input values after form submit

Time:09-16

I have a view:

What I want is that whenever I submit a form (if request.POST) whether the form is valid or not, the values of the inputs do not disappear.

The inputs event_form.description, event_form.event_type, event_form.dfkl_id keep their values after clicking submit, but for some reason the value of the datetimepickers disappears.

How can I make the input value in datetimepickers not disappear when submitting the form?

CodePudding user response:

You need to return the same form which has been validated, in order to return the same input content and form errors. Also relocate your POST request after the context data is defined (After data) Like so:

if request.POST:
    event_form = EventForm(request.POST, instance=event)
    if event_form.is_valid():
        [... validate logic ...]
        return HttpResponseRedirect('/events')
    data.update({'event_form':event_form})
    return render(request, template_name, data) 

Now if event_form does not validate, then it has a route to be returned.

  • Related