Juan Carlos Asuncion
Juan Carlos Asuncion

Reputation: 967

Django form.save() to another class/function

How do I .save() to another class/function like:

def save_page(request):

    # contains 3 fields

    form = sample_tableform(request.POST)
    if request.method == 'POST':
        if form.is_valid():

    #send a confirmation code to email
    #redirect to code confirmation page

def valcode(request):

    #contains 1 field

    sub_form = confirmform(request.POST)
    if request.method == 'POST':

        if sub_form.is_valid():

            #compare if code from save_pages matches the input field
            # if it matches then the data can be stored

            form.save()
            subform.save()

Is it possible or no?

I like the idea of using sessions but how do i implement it?

Upvotes: 1

Views: 289

Answers (2)

Cody Bouche
Cody Bouche

Reputation: 955

import json, uuid

# models.py
from django.db import models

class ValidationCode(models.Model):
    confirmation_code = models.CharField(max_length=255, unique=True)
    form_data = models.CharField(max_length=255, unique=True)
    create_datetime = models.DateTimeField(auto_now_add=True)

# views.py

def save_page(request):

    # contains 3 fields

    form = sample_tableform(request.POST)
    if request.method == 'POST':
        if form.is_valid():
            confirmation_code = uuid.uuid4()
            validation_code = ValidationCode(confirmation_code=confirmation_code, form_data=json.dumps(request.POST))
            validation_code.save()
            # send email with the code here    
            #redirect to code confirmation page

def valcode(request):

    #contains confirmation_code field

    sub_form = confirmform(request.POST)
    if request.method == 'POST':
        if sub_form.is_valid():
            try:
                code = ValidationCode.objects.get(confirmation_code=form.fields['confirmation_code'])
                tableform = sample_tableform(json.loads(code.form_data))
                if tableform.is_valid():
                    tableform.save()
            except ValidationCode.DoesNotExist: 
                # return error

Upvotes: 0

Agate
Agate

Reputation: 3232

Assuming your code comes from your views.py, the answer is you can't.

Views does not work like that (neither does HTTP). If you want to access data between subsequent HTTP requests, you'll have to persist it somewhere.

In your case, you'll probably need to store the validation code in database with a model:

# models.py
from django.db import models

class ValidationCode(models.Model):
    code = models.CharField(max_length=10, unique=True)
    creation_date = models.DateTimeField(auto_now_add=True)
    is_validated = models.BooleanField(default=False)

# views.py

def save_page(request):

    # contains 3 fields

    form = sample_tableform(request.POST)
    if request.method == 'POST':
        if form.is_valid():

    validation_code = ValidationCode(code="random_code_here")
    validation_code.save()

    # send email with the code here    
    #redirect to code confirmation page

def valcode(request):

    #contains 1 field

    sub_form = confirmform(request.POST)
    if request.method == 'POST':

        if sub_form.is_valid():

            try:
                code_from_db = ValidationCode.objects.get(code=form.fields['code'])
            except ValidationCode.DoesNotExist: 
                # Not code matching input exist in database
                # return some error to the user

            # code exist in db, just mark it as validated
            code_from_db.is_validated = True
            code_from_db.save()

Upvotes: 1

Related Questions