Django Button Behavior within View that Handles POST and GET Requests

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.

Django and VSCode

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})

Have you worked your way through the Official Django Tutorial? I always recommend people start with that - it provides a lot of the fundamentals you need to understand to work with Django.

Beyond that, you can read:

If this isn’t clear, if you’re still not understanding what’s going on, then you might want to find some tutorial materials that discuss “The HTTP request / response Cycle”. This goes beyond Django - all web-based applications are built on the HTTP protocol. You will especially want to be clear on what happens in the browser as opposed to what’s happening in the server.

Thanks, Ken.

In hindsight this is just one behavior of many that I don’t understand, however I’m okay with not understanding it right now. But thank you for the pointer on revisiting the tutorials and providing the links. I have previously worked through a udemy course which really helped me, but it was a while back.

I think I’ll learn these things as I build my site. And I’ve already made a lot of progress since I posted this. However, because django is very user friendly, understanding the details of the http exchanges between forms and views hasn’t risen to the highest priority yet. I’m sure it will eventually.

Thanks again,
Andy