Form not posting to view

Trying to capture the submit button action in HTML for a form to view. It seems like it is not getting to the view.

views.py

class FisherAgreed(TemplateView):
    form_class = forms.FisherAgreement
    success_url = reverse_lazy("login")
    template_name = "fisher_agreement.html"


    def form_valid(request):
        print(request.post)

Template

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<form method="post">
    {% csrf_token %}
    {{ form.as_p }}

<input type="submit", value="Agree to all" name="agree"> <input type="submit" value="Cancel" name="cancel"/>
</form>
</body>
</html>

forms.py

class FisherAgreement(TemplatesSetting):
    form_template_name = "fisher_agreement.html"

Yes, because a template view is not used for form submissions. You need to use FormView or one of its descendant classes such as CreateView.

Tried both and still nothing.

Also, I don’t have to use a form, I would prefer to capture posts from the template.

Just saying that “Something isn’t working” provides no information allowing us to help you.

You need to post the code that you are trying, along with a description of what’s happening, including any error messages that you may be receiving.

Also, regarding your latest post here, you are correct. Likewise, you don’t need to use Django at all. But if you’re going to use Django, you might as well learn how to use it properly, along with gaining an understanding of how it works.

Understand and I am trying to learn. I do research for answers before I post here.

All I want to do is capture the submit button name on a template and then process based on that. If they agree, do this, if they cancel, do this.

If form is the right way to go then that is the way I would like to proceed. Does it need to be a class view or can it be a function?

form:

class FisherAgreement(FormView):
    form_template_name = "fisher_agreement.html"

template:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<form method="post">
    {% csrf_token %}
<input type="submit", value="Agree to all" name="agree"> <input type="submit" value="Cancel" name="cancel"/>
</form>
</body>
</html>

view:

class FisherAgreed(FormView):
    form_class = forms.FisherAgreement
    success_url = reverse_lazy("login")
    template_name = "fisher_agreement.html"


    def form_valid(request):
        print(request.post)

Please let me know what other information you need.

If you’ve got no data at all associated with these buttons, then no, you don’t need a full form. I’d create the two buttons using the formaction attribute in each to link to two separate views - each of those views would take the appropriate action. (I’d also make it a GET and not a POST)

If you really can’t separate the functionality out into two views, then you can retrieve the value from those buttons if you use the same name attribute on those buttons. For example, if you set name="Submit" on both buttons, you would get the value of that button from request.POST['Submit'] (Note that it’s request.POST, not request.post.

Side note: There are no functional differences between a CBV and an FBV. Whatever you can do in one, you can do in the other. The differences are a matter of code structure and not of functionality.

Also, if you’re still learning about the CBVs, I suggest you become very familiar with the Classy Class-Based Views site along with the CBV diagrams page. The two of them are excellent resources for getting a real understanding of how CBVs are constructed and work.

Additionally, you have:

This is not valid, because you’ve defined it as:

Which defines FisherAgreement as a view and not a form.

Ok thank you. Just can’t seem to get my head around the flow.

So here is what I have now. Just want to see the post info and I can take it from there.

view

class FisherAgreed(FormView):
    form_class = forms.FisherAgree
    success_url = reverse_lazy("login")
    template_name = "accounts/fisher_agreement.html"
    model = CustomUser


    def form_valid(request):
        print(request.POST)

form

class FisherAgree(forms.Form):
    form_template_name = "accounts/fisher_agreement.html"

template

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<form method="post">
    {% csrf_token %}
    {{ form.as_p }}

<input type="submit", value="Agree to all" name="agree"> <input type="submit" value="Cancel" name="cancel"/>
</form>
</body>
</html>