Skip to content

Commit

Permalink
31 - Handling Form Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
codingforentrepreneurs committed Dec 16, 2019
1 parent 05d048d commit 31429a1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
Binary file modified db.sqlite3
Binary file not shown.
8 changes: 8 additions & 0 deletions templates/pages/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,17 @@ <h1>Welcome to Tweetme 2</h1>
const ogHtml = tweetsContainerElement.innerHTML
tweetsContainerElement.innerHTML = newTweetElement + ogHtml
myForm.reset()
} else if (xhr.status === 400) {
const errorJson = xhr.response
console.log(errorJson)
} else if (xhr.status === 500) {
alert("There was a server error, please try again.")
}

}
xhr.onerror = function() {
alert("An error occurred. Please try again later.")
}
xhr.send(myFormData)
}
const tweetCreateFormEl = document.getElementById("tweet-create-form")
Expand Down
4 changes: 4 additions & 0 deletions tweets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def home_view(request, *args, **kwargs):
return render(request, "pages/home.html", context={}, status=200)

def tweet_create_view(request, *args, **kwargs):
print(abc)
form = TweetForm(request.POST or None)
next_url = request.POST.get("next") or None
if form.is_valid():
Expand All @@ -25,6 +26,9 @@ def tweet_create_view(request, *args, **kwargs):
if next_url != None and is_safe_url(next_url, ALLOWED_HOSTS):
return redirect(next_url)
form = TweetForm()
if form.errors:
if request.is_ajax():
return JsonResponse(form.errors, status=400)
return render(request, 'components/form.html', context={"form": form})


Expand Down

0 comments on commit 31429a1

Please sign in to comment.