Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

django: How do i save form in django without the django forms?

I am trying to save a contact form, but i am not using the django form but instead the input field.
I am getting the name, email address and message body from the html templates using input field like this <input type="text" id="name"> but the form doesn’t get any data from the input field, it seems i am missing something but i cannot find out what is happening.

I have tried passing in the name, email and message into id="name" id="email" id="message" but nothing still happends.

This is my code
index.html

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<form method="POST">
    {% csrf_token %}

    <input type="text" id="name" value="" />
    <input type="text" id="email" value="" />
    <input type="text" id="address" value="" />

    <button type="button">Let's Withdraw</button>
</form>

views.py


if request.method == "POST":
    name = request.POST.get("name")
    email = request.POST.get("email")
    message= request.POST.get("message")
    print("Message Sent")
    return redirect("core:index")
else:
   pass

>Solution :

This is your form

<form method="POST">
    {% csrf_token %}

    <input type="text" id="name" value="" />
    <input type="text" id="email" value="" />
    <input type="text" id="address" value="" />

    <button type="button">Let's Withdraw</button>
</form>

change to this

<form method="POST">
    {% csrf_token %}

    <input type="text" id="name" name="name" value="" />
    <input type="text" id="email" name="email" value="" />
    <input type="text" id="address" name="message" value="" />

    <button type="submit">Let's Withdraw</button>
</form>
  1. add the name=" ... " attribute in your input field and the corresponding values e.g name, email, address.

  2. Change your button type to submit as your would be submiting a form

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading