0

I have used Django to set up the backend of a web app and have an HTML form I am using as a signup page and was wondering if there's anything I need to add to the Forms' form action attribute. What I am trying to do is grab the data that the user has posted it and send it over to PGadmin database.

Here is my html form for reference:

<div>
  {% block content %}
  <form action="" method="post" name="Userinfo" id="Userinfo">
    <label for="fname">First Name</label>
    {% csrf_token %}
    <input type="text" id="fname" name="firstname" placeholder="Your first name..">
    <label for="lname">Last Name</label>
    <input type="text" id="lname" name="lastname" placeholder="Your last name..">

    <label for="DOB">Date Of Birth</label>
    <input type="text" id="DOB" name="DOB" placeholder="The Date you were born">

    <label for="Email">Email Address</label>
    <input type="text" id="Email" name="Email" placeholder="Your email address">


  
    <input type="submit" value="Submit">
  </form>
  {% endblock %}
    </div> 

P.S this isn't all the whole file, just a snippet.

Also here is the views.py file that I have, which makes any imputed data get processed as a POST request vs as a GET. Not sure if there is anything I need to add in order to make it send the data to PGadmin.

class CommentForm(forms.Form):
fname = forms.CharField(max_length=25)
lname = forms.CharField(max_length=25)
dob = forms.datefield()
email = forms.EmailField()

if request.method== "POST":
    form = RegisterForm(request.POST)
    if form.is_valid():
        cd = form.cleaned_data
        Info = form(
            fname = cd['name'],
            lname = cd['lname'],
            dob = cd['dob'],
            email = cd['email'],
    )
    form.save()  

I have my views setup to where I have a separate app that process the backend stuff for the sign up and another that's sole purpose is to link to the right pages and links to the HTML file mentioned earlier.

Here it is, just in case

def register(request):
 return render(request,"register/userinfo.html")

As usual, any help is greatly appreciated.

  • Note that PGAdmin is an administration tool for the PostgreSQL database. I think this really does have everything you need. It is [a best practice](https://stackoverflow.com/questions/1131781/is-it-a-good-practice-to-use-an-empty-url-for-a-html-forms-action-attribute-a) to leave out the action attribute of the
    tag if your form is submitted to the same view that sent it originally.
    – Jeff Booth Feb 02 '22 at 03:43
  • If the same view function 'renders' as well as 'processes' the form, then you can leave the action field empty. Once you submit the form, the data will go the function which rendered the form. If there is another function which will process the form data, then you will have to specify the function in the 'action' attribute. – Chandragupta Borkotoky Feb 02 '22 at 04:26
  • Ah, I have it set to where my main views.py has a function to render the form and I have a separate app that has it's own views.py file that manages it. – David_Codes Feb 03 '22 at 00:24
  • So how would I go about linking the form to the function in my view.py file? – David_Codes Feb 03 '22 at 01:08

0 Answers0