I'm trying to set up a form in Django and save the data to my database, without using a ModelForm. My form is working, but the part I am stuck on is how to process the form data and save it within the view. As you can see, after 'if form.is_valid():' I am stuck and cannot think of the right code.
# models.py
from django.db import models
class Listing(models.Model):
business_name = models.CharField(max_length=80)
business_email = models.EmailField()
business_website = models.CharField(max_length=80)
business_phone = models.CharField(max_length=80)
# forms.py
from django import forms
class NewBusinessForm(forms.Form):
business_name = forms.CharField(label='Business Name', max_length=100)
business_phone = forms.CharField(label='Phone Number', max_length=100)
business_email = forms.EmailField(label='Email Address', max_length=100)
business_website = forms.CharField(label='Web Site', max_length=100)
# views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
from .forms import NewBusinessForm
def new_business(request):
if request.method == 'POST':
form = NewBusinessForm(request.POST)
if form.is_valid():
# process form data
return HttpResponseRedirect('/')
else:
form = NewBusinessForm()
return render(request, 'directory/new.html', {'form': form})