Skip to content
Advertisement

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

<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

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement