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!
Thanks to mariodev for pointing me in the right direction.
I found this combination of code to work for my application:
[urls.py]
[views.py]
[forms.py]
However this does create a new issue. I can now visit the first form and the value from sheet_id_initial in the url pattern has been added in the form field as expected, but when the submit button is pressed a 404 page not found is returned due to POST not adding the sheet_id_initial portion to the request url.
[returned page]
""""
Page not found (404) Request Method: POST Request URL: http://192.ip.address.1:8001/sheets/entry_form/
Using the URLconf defined in first_project.urls, Django tried these URL patterns, in this order:
The current URL, sheets/entry_form/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
""""
[ADDITIONAL INFO]
To change the way from which to attain the sheet_id variable from either a URL pattern or POST data is rather straight forward:
First change the regex pattern in urls.py to a simple match like:
Then in views.py change the assignment in the dispatch override method like so:
Finally if you're getting MultiValueDictKeyError, I added a try-except block to get_form_initial() that tests for the existence of the the variable declared from the dispatch method if the POST data was present (but I'm not sure if this is the best way to handle this..)
so in views.py add these lines to get_form_initial():
[UPDATE - Best Fix So Far]
This sessionwizard and class based views can be a bit tricky. I found that both of those previous approaches have some flaw in one way or another and realized I was over complicating things a bit. But most of the previous code was essential but instead of POST or URL patterns I'm storing the POST data in a session and accessing it across forms at different URLs!!
now I have a form that the user starts from at say the address: "http://192.ip.address.1:8001/sheets/start/" which has two fields in the form: "part_id_initial" and "sheet_id_initial"
when submit is pressed the user is redirected to: "http://192.ip.address.1:8001/sheets/entry_form/" however there is a restriction of HTTP that POST data cannot go with redirects.
so you can save the session POST data and use it in the another form by modifying the above code like so:
urls.py
views.py
The rest is pretty much exactly the same as above. Saving the POST data in to the session has been cleanest and most elegant fix I've found thus far.
Hope this helps!!
Use the wizard's
dispatch
method:Then use
self.sheet_id
withinget_form_initial
method to populate initial value for your form.