user9558965
user9558965

Reputation:

How to get form input using forms.Form into a postgres database?

Here is my code:

My forms.py looks like:

from django import forms


class SimpleForm(forms.Form):
    website = forms.URLField(required=True,
                             widget=forms.TextInput(
                             attrs={'placeholder': "http://www.example.com"}))
    email = forms.EmailField(required=True,
                            widget=forms.TextInput(
                            attrs={'type': 'email',
                                   'placeholder': "[email protected]"
                                  }))

My views.py looks like:

from django.shortcuts import render, redirect
from django.template import loader

from .forms import SimpleForm


def simple_form(request):
    if request.method == 'POST':
        form = SimpleForm(request.POST)

        if form.is_valid():
            website = form.cleaned_data['website']
            email = form.cleaned_data['email']

            return render(request, 'some_page.html')
    else:
        form = SimpleForm()

    return render(request, 'some_other_page.html', {'form': form})

As for my HTML form, it looks like the following below:

<form method="post">
  {% csrf_token %}
  <div class="some-class">
    <label for="website">Enter Your Site:</label>
    <!-- <input type="text" id="website" placeholder="http://www.example.com" name="website" /> -->
    {{ form.website }}
    <label for="email">Enter Your Email:</label>
    <!-- <input type="text" id="email" placeholder="[email protected]" name="website" /> -->
    {{ form.email }}
  </div>
  <div class="some-other-class">
      <div class="another-class">
        <button name="submit">Submit</button>
      </div>
  </div>
</form>

My question is, how do I get the input for this form into a table in my postgres database?

Upvotes: 0

Views: 1161

Answers (1)

Nebulosar
Nebulosar

Reputation: 1865

You could make a ModelForm instead of a form and turn your form into a Model:

class SimpleModel(models.Model):
    website = models.URLField(
        required=True, 
        widget=forms.TextInput(attrs={'placeholder': "http://www.example.com"}))
    email = models.EmailField(
        required=True,
        widget=forms.TextInput(attrs={'type': 'email', 'placeholder': "[email protected]"}))

class SimpleForm(forms.ModelForm):
    class Meta:
        model = SimpleModel
        fields = ['website', 'email']

Follow the link and look at the docs, you can make it very abstract, Django can handle most things for u.

When saving you can do a form.save() as seen here

It is wise to first check the form by doing form.is_valid()

Example view:

def simple_form(request):
    if request.method == 'POST':
        form = SimpleForm(request.POST)

        if form.is_valid():
            form.save()
            return render(request, 'some_page.html')
    form = SimpleForm()
    return render(request, 'some_other_page.html', {'form': form})

You can also follow this tutorial to get to know the Django basics Sounds like you could use the basics ;)

Upvotes: 1

Related Questions