0

I have two urls say A.html and B.html with views a and b respectively. A has a form action which points to B.html. So A.html's action points to view b and B.html's also points to view b.

I have two issues:

  1. In case the user does not select a value in A.html and presses submit button then it should point to the same url as of A.html. But due to my form action it gets directed to B.html. Is there any way of maintaining this direction to A.html only.

  2. The term selected in A.html passes to B.html. I need to preserve this term since it has an usage while submission of another form which is in B.html. My issue is B.html points to same view and A.html's form action also points to same view. While posting form B, I need to take the term from A.html plus the term selected from B.html while maintaining the checks if for example, nothing was selected in B.html and still the form is posted. How to do this?

2
  • Can you provide any kind of code sample for what you're attempting to accomplish? This question is very ambiguous; it's going to be rather difficult for anybody to help without knowing more. Commented May 6, 2015 at 17:59
  • I can't provide a code sample. Proprietary issues. Commented May 6, 2015 at 18:48

1 Answer 1

2

If I understand correctly, you are trying to achieve a step by step form validation.

For this purpose a clean and simple way to save data between different steps is to use WizardView from Django's Contrib : It allows you to choose to save form data in session or with cookies and will handle form validation as you expect

Inspired from the docs in your case :

in forms.py

from django import forms

class FormA(forms.Form):
    field_1 = forms.CharField(max_length=100)

    #Usual validation methods here if needed

class FormB(forms.Form):
    field_1 = forms.CharField(max_length=100)

    #Usual validation methods here if needed

in views.py

from django.shortcuts import render_to_response
from myapp.forms import FormA, FormB
from django.contrib.formtools.wizard.views import SessionWizardView

FORMS = [("form_a", FormA),
         ("form_b", FormB)]

TEMPLATES = {"form_a": "myapp/a.html",
             "form_b": "myapp/b.html"}

class MyFormWizard(SessionWizardView):

    def get_template_names(self):
        return [TEMPLATES[self.steps.current]]

    def done(self, form_list, **kwargs):
        return render_to_response('done.html', {
            'form_data': [form.cleaned_data for form in form_list],
        })

in urls.py

from django.conf.urls import url, patterns

from myapp.views import MyFormWizard

urlpatterns = patterns('',
    (r'^myform/$', MyFormWizard.as_view(FORMS)),
)

You can use this Wizard with modelforms and formsets.

Let me know if this helps!

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.