I’ve implemented the code on the site at the link below. I’m trying to understand what is happening with the log_message view. I understand that the view below handles both POST and GET requests. Note, in the template that this log_message calls (log_message.html), there is a form string/char field called message and a button to log the message.
So, on the first call to this view (view.log_message), the first if is passed and the render function is called where it passes the form to the template. The template contains a message field and log button. I insert some text into the message form, and then press the log button. While doing this in VSCode debugger, i notice that when i press the log button, the control is sent back to the start of the log_message function, but this time request.method==‘POST’. There seems to be some underlying behavior that when the button is pressed it reloads the page and thus calls the log_message view again? But that doesn’t seem very transparent in the code.
Can someone explain what’s going on here?
def log_message(request):
form = LogMessageForm(request.POST or None)
if request.method == "POST":
if form.is_valid():
message = form.save(commit=False)
message.log_date = datetime.now()
message.save()
return redirect("home")
else:
return render(request, "dashboard/log_message.html", {"form": form})