Django form ignore validations on save vs submit - django

I am trying to create a Django form in which the user will have two options
Save the form (temporarily)
Submit the form
What I am trying to do is to allow the users to save the form temporarily and after they have all the data they can come back, edit the form and make the final submission.
The problem is that the form validation will not allow partially filled form to be saved.
Is there any way I can ignore validations when "Save" is pressed whereas perform all the validations when "Submit" is pressed?
I do not want to have 2 views. I want the same view with two buttons "Save" and "Submit" if Save is pressed some of the validations will be ignored (i.e. required field left empty due to the fact that the user might not currently have the information). But if the "Submit" is pressed, all the date should be validated.
Please note that all the validations takes place backend with Django.
Thanks

Related

Django: Redirect to the referring page (not the current form) when an UpdateView fails validation and is then Canceled?

My Django application allows a user to navigate to an Update Form (UpdateView) from multiple locations in the application. Currently, if the form is saved successfully I can redirect without a problem to the referring page by saving request.META.HTTP_REFERER in a hidden field in the template and retrieving it in form_valid. I can leverage the HTTP_REFERER data in MOST, but not ALL cases. One of those is the following scenario:
My user navigates to the FORM A from Page X (HTTP_REFERER is set to Page X)
FORM A is submitted and does not pass validation (HTTP_REFERER is now set to FORM A instead of Page X because indeed it has self-refered.)
My user clicks Cancel (they are redirected back to FORM A instead of Page X)
Is there a way to capture the Cancel event before it is processed and perform some logic so my user gets referred back to Page X instead of being stuck in the form?

How to implement a sequence of forms

New to Django here. I want to implement two forms in a sequence, where the second form depends on the saving of the first form. For example: View_A implements GET / POST methods of form A. Once Form A is submitted and saved, it will generate a unique ID. Form A also has a radio button with two choices (B and C). Depending on the choice made, I need to present the user with a second form B or a form C, where the unique ID from form A is filled automatically. The prerequisite for forms B and C is that the unique ID on form A is present. I can't use AJAX, because the form A needs to be saved first.
My problem is, as soon as I hit the submit button of form A, it becomes a POST request. I can generate the unique ID and display it in the returned html template. However, it's still the same view and I don't know how the display of next form. The display of form B/C should ideally be a GET request. But I am in the POST request.
I hope this is a clear enough question.
Thanks
You would have to implement three views, one for each form. When the users saves form A, you check that the form is valid and generate the identifier. After that, just return a HttpRedirect. If the identifier is used for the process, you also give it as a parameter to the next view.
def my_view(request):
# Construct your form from the POST data
if form.is_valid():
return redirect('next-form-view', identifier=your-generated-identifier)
Documentation about the redirect shortcut can be found here.

Saving Formset in a Session - Django

Is here any way to store formset to a session ?
My Scenario is like this . I have a form to fill user data and upload user certificates, and in the next page(form by clicking next) there is a form to enter Professional details .
Is it possible to limit Maximum number of forms generated using a formset?
If I understand your question correctly - how to save a state of the from in a session, then starting with Django 1.4, it actually comes with a way on how to do that out of the box.
https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/
It allows you to split a form into multiple section, which then user can fill separately. Once user fills any one section, he/she go to the next page, at which point the state of the form will be saved in a session. Once all the pages are filled, then everything can be saved to a database.
In addition, while going from one page to the other, you add logic of what should be on the next page.
Image that you have a wizard where on the first page it asks what type of content user wants to upload. Then upon going to the second page, then depending on the answer from the first page, appropriate upload fields can be present - field for video, music, or graphics.
I would have answered FormWizard but if you don't want to use it, you can simply create two forms. when the user submit the first one, you pickle it into a session and then you generate the second form. When he clicks on back link, you unPickle saved data and you prefill the form.
def submitFirstForm(request):
data = request.POST['data']
import cPickle
request.session['data'] = cPickle.dumps(data)
...
def backBtn(request):
import cPickle
data = cPickle.loads(request.session['page'])
form = DataForm(data)
...

django forms - remove "submit" button

I have a simple form that enables selecting an option (radio button). Upon selection the form automatically submits itself (using the onchange attribute):
class MyForm(forms.Form):
choices=forms.ChoiceField( widget=forms.RadioSelect(attrs={'onchange': 'this.form.submit();'}), choices=[(k,k) for k in options],label="choose one")
This is great, only problem is I still have the "submit" button at the bottom of the form when it's rendered. Is it possible, without creating a template, to somehow make the form render without a submit button?
From Django doc:
The form only outputs its own fields;
it is up to you to provide the
surrounding tags and the submit
button.
It means that you probably have the submit button in you template - remove it if you don't need it.

Sending user to a page created based on their POST request in Django

I have a form in which the user selects a few items to display on "the following page". This selection is always unique, and we will store each set of selections made using a model, indexed by the id as is par with Django models. When the user selects her choices and POSTs using the submit button, I would like for our application to store her selections in the model, and then render a page with the id of the model so that the user can get back to the page she created at any point with a simple GET request.
For example, the user goes to /coolapp/selectprefs/, makes a few selections, and clicks submit. The user should then be taken to /coolapp/selections/42 given that when the user submitted and created the record, the record was given an id of 42.
What I don't understand is how to send the user to "the following page" as a response (e.g., /coolapp/selections/42 in the above example) after she clicks the submit button. Taking a user to a page of a unique ID based on what she entered seems like a common task (e.g., it will happen when I click the button to submit this question on SO), but I'm not sure how to go about doing it, and would appreciate your advice.
Return a HttpResponseRedirect from your view that handles the POST.