I've pretty much exhausted my resources on this subject, so I must submit a question to the community.
Scenario:
In my urls.py file i have a pattern like:
url(r'^entry_form/(?P<sheet_id>\d+)/', SheetWizard.as_view([Sheet1,Sheet2,Sheet3])),
When a user visits a url like "127.0.0.1/myapp/entry_form/77" I am trying to have Django render Sheet1 but with one of the fields having the value "77" initially entered.
Theroy:
My forms.py file looks similar to:
class Sheet1(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(Sheet1, self).__init__(*args, **kwargs)
#create a helper object
self.helper = FormHelper(self)
#dont render the form tags
self.helper.form_tag = False
#Make a nice layout
self.helper.layout = Layout(
TabHolder(
Tab(
'Sheet Information', #Tab name text
PrependedText('sheet_field', 'Django-'), #The field with prepended text
)
)
#Now set the initial value of the field to sheet_id from url pattern
self.fields['sheet_field'].initial = str(sheet_id)+'-'+ str( time() ).replace('.','_')
#??? Not sure where to get sheet_id from???
Notice the last line has a variable named "sheet_id", that should be the value "77" coming from the url pattern entered by the user.
Problem:
So far I am unsure of how to access the value "sheet_id" from the url pattern in my forms.py or views.py files. Due to this being a class based view I can not simply create the keyword "sheet_id=None", aka something like this just doesn't work:
class SheetWizard(SessionWizardView, sheet_id=None):
#this breaks
I have been able to get some data into views.py using request.GET and a url like "127.0.0.1/myapp/entry_form/?sheet_id=77" but I have no idea how to pipe that into the first form of the SessionWizardView user session.
It would be greatly appreciated if someone could help me out. And thanks for all your kind wisdom!
Use the wizard's dispatch
method:
def dispatch(self, request, *args, **kwargs):
self.sheet_id = kwargs.get('sheet_id', None)
return super(SheetWizard, self).dispatch(request, *args, **kwargs)
Then use self.sheet_id
within get_form_initial
method to populate initial value for your form.