Wednesday, March 20, 2013

Re: Help with Django form wizard.

On Thu, Mar 7, 2013 at 9:21 PM, Dilip M <dilipm79@gmail.com> wrote:
Hi,

Here is what I am trying to do. Get the root folder name in form 1 and list the found sub directories (matching a pattern) and give an
option for user to select the sub dirs. Once selected,  I want to save the model.

1. How to dynamically display found sub directories in form 2. I have defined a method (def _findSubs) in views.py
2. I have split a model into 2 forms. How to save the data fetched from 2 forms into one model.
3. I have read the docs. But couldn't figure out where to put WizardView.get_form_initial() method. My knowledge with Django is very trivial.

Here is what I am doing.

--------------------
models.py
-------------------

from django.db import models

class Tree(models.Model):
    rootDir = models.CharField(max_length=100) # will be populated using form 1
    subDirs = models.CharField(max_length=100) # will be populated using form 2

    def __unicode__(self):
        return u'%s %s' % (self.rootDir, self.subDir)

  
--------------------
views.py
-------------------

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.contrib.formtools.wizard.views import SessionWizardView
from myapp.forms import MainForm, SecondForm


FORMS = [("main", MainForm), ("select", SecondForm)]
TEMPLATES = {"main": "main.html", "select": "select.html"}

class FormWizard(SessionWizardView):

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

        def get_form_kwargs(self, step):
                if step == '
select':
                    kwargs = {}
                    kwargs.update({'choices': (('1', 'Option1'), ('2', 'Option2'))})

                    return kwargs
                else:
                    return {}

       
Now I get

File "C:\Python27\lib\site-packages\django\contrib\formtools\wizard\views.py" in render_next_step    302.             files=self.storage.get_step_files(next_step))  File "C:\Python27\lib\site-packages\django\contrib\formtools\wizard\views.py" in get_form    395.         return self.form_list[step](**kwargs)    Exception Type: TypeError at /  Exception Value: __init__() got an unexpected keyword argument 'choices'  

-------------------
forms.py
-------------------

from django import forms
from myapp.models import Tree

.
.

class SecondForm(forms.Form):
    SUBDIRS = #
_findSubs result should be here.
    SELECTED = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices = SUBDIRS)


 
    def done(self, form_list, **kwargs):
        # Now save Model here. HOW??
        pass


def main(request):

    if request.method == 'POST':
        http_req_type = request.method
        mainform = MainForm(request.POST)

        if mainform.is_valid():
            cd = mainform.cleaned_data  
            tree = cd['rootDir']
   subdirs = _findSubs(tree)
    else:
        http_req_type = request.method
        mainform = MainForm()

    return render(request, 'main.html', locals())

def selection(request):
    if request.method == 'POST':
        http_req_type = request.method
        selectionform = SecondForm(request.POST)
       
        if selectionform.is_valid():
            cd = selectionform.cleaned_data
            subdirs = selectionform.cleaned_data.get('SELECTED')
            return HttpResponseRedirect('/done/')
    else:
        http_req_type = request.method
        form = SecondForm()
                                   
    return render(request, 'select.html', {'form':form })

def _findSubs(tree):
    ''' Return a list of sub directory matching the pre-defined patter
    '''
    # some logic here. Don't worry abt it.
    return subs


-------------------
urls.py
-------------------
from myapp import views

    url(r'^main/', views.FormWizard.as_view(views.FORMS)),


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment