0

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?

5
  • 1
    Do you have a model? If so, why don't you use a model form? And if not, how are you expecting the data to go into the db? Commented Mar 27, 2018 at 14:55
  • @DanielRoseman I just followed this docs.djangoproject.com/en/2.0/topics/forms Commented Mar 27, 2018 at 14:58
  • Well that's just one part of it. Rather than reading a single page of the documentation, you should follow the tutorial. Commented Mar 27, 2018 at 14:59
  • @DanielRoseman what tutorial? Commented Mar 27, 2018 at 15:00
  • docs.djangoproject.com/en/2.0/intro/tutorial01 Commented Mar 27, 2018 at 15:16

1 Answer 1

1

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 ;)

Sign up to request clarification or add additional context in comments.

1 Comment

How to i obj.save() this if my views have a website and email under the conditional? do I do form.save() anyway?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.