How to create alert from view model

Viewed 29

I want to validate file. While file is invalid, i want to refresh my page and inform user that he did not upload proper file. So i have this in my

views/campaign.py

try:
    wb = load_workbook(mp_file)
except BadZipfile:
    return redirect('campaign_add', client_id)

The only way i know how to do it is add another attribute to client class which will be

is_error(models.BooleanField())

And then change views/campaign to

try:
    client.is_error = False   
    wb = load_workbook(mp_file)
    client.save()
except BadZipfile:
    client.is_error = True
    client.save()
    return redirect('campaign_add', client)

And with another attribute i can add in my campaign.html file some kind of if is.error is true i'm adding some kind of windows with information about bad file after reloading page. But is there any way to do it without adding another attribute?

1 Answers

Ok, let's imagine that the answer is a little bit complicated than you've expected.

Modern UI's are not reloading pages just to inform about some errors with user input or upload.

So what is the best user experience here?

  1. User is uploading some file(s) from the page.
  2. You are sending a file via JavaScript to the dedicated API endpoint for this uploading. Let's say /workbook/uploads/. You need to create a handler for this endpoint (view)
  3. Endpoint returns 200 OK with the empty body on success or an error, let's say 400 Bad Request with detailed JSON in the body to show to the user what's wrong.
  4. You're parsing responses in JavaScript and show the user what's wrong

No refreshes are needed.

But the particular answer will need more code from your implementation. (view, urls, template)

Related