So I have the following files:
Views.py:
def home(request):
return render(request, 'home/index.html')
def add(request):
if request.method == 'POST': # If the form has been submitted...
# ContactForm was defined in the previous section
form1 = AddEventForm(request.POST) # A form bound to the POST data
#person = AddPersonForm(request.POST) # A form bound to the POST data
if form1.is_valid(): # All validation rules pass
form = form1.save()
request.session['event_data'] = request.POST
#request.session['event_data2'] = form
return HttpResponseRedirect('/add_person/')
#return HttpResponseRedirect(reverse('/add_person', kwargs={'PK':form.pk}))
else:
form1 = AddEventForm() # An unbound form
person = AddPersonForm() # A form bound to the POST data
return render(request, 'add/add.html', {
'form1': form1, 'person': person,
})
def add_person(request):
event_data = request.session.get('event_data')
if request.method == 'POST':
person = AddPersonForm(request.POST)
else:
person = AddPersonForm()
return render(request, 'add_person/add_person.html',{'event_data': event_data, 'person': person,})
models.py:
class event(models.Model):
event_name = models.CharField(max_length=200)
date = models.DateField('Date',default=date.today)
event_id = models.AutoField(primary_key=True)
admin_username = models.CharField(max_length=50)
def __unicode__(self):
return ('%s %s %d') % (self.date, self.event_name, self.event_id)
class Person(models.Model):
eventID = models.ForeignKey(event)
first_name = models.CharField(max_length=50)
second_name = models.CharField(max_length=50)
email = models.EmailField(max_length=50)
phone = models.CharField(max_length=25)
company = models.CharField(max_length=200)
favourite = models.BooleanField(default=False)
def __unicode__(self):
return eventID
This problem has been bugging me for hours. I have an initial page where the user enters some information about an event. If the checks pass the form saves and a primary key is generated. The user is then redirected to another page where they fill out a form about the people they have met there. The problem is I cannot find a way of passing the primary key to the second page (add_people
). I need the primary key because it is my foreign key in my Person
table and I need to use to pre-populate the eventID
in the add_people
page.
I initially thought the primary key would be in the request.POST
after the form is saved but its not.