I am building a review form that will post to a single model in my database. This form will have multiple sections, each with its own set of questions. I want the user to be able to complete a section and hit a submit button that will save the progress and redirect them to the next section. In essence I want to create an in progress status for the review with the idea that as all sections are finished the user can hit a complete button that will run my validation on the entire form. However, I do not want to allow posting information to the database that has not been cleaned.
I've been thinking through this trying to work out what I need to do and believe the best bet would be to have a complete button on the last page that changes blank to False for the fields of the form. I believe this would allow me to fill out each form and post it to the database as all fields would start as optional but then for that specific model instance the click of the button at the end would institute a change making all fields required, thereby running validators to ensure the form is complete. I believe this will also allow for saving progress and returning to it later if interrupted during completion of the form.
Can anyone provide any insight on if this is even possible? If so does anyone have examples that could guide me?
You can store the unvalidated data to the user's session. The final stage pulls all previous step data out of the session for validation. IIRC the Django form wizard does just this so you don't have to write it.
I might instead do this client-side, using one big (probably Crispy) form, and some client-side pseudo-submit buttons that just invoke Javascript to hide one section of the big form and reveal the next. The final one is a real submit, and redisplay of the whole thing if there are errors.
I'd like to put a search icon (like magnifiying glass) beside a field in the admin change form to let the users to trigger a script to fill other fields on demand only. I have to avoid triggering any field event (blur, click, change etc) on this field because it has to be triggered under users request, according to their needs.
Django has its own magnifying glass icon/link for raw_field foreign key selection popup. Is it possibble to do something similar, where I would trigger the script upon the magnifying glass click. It will be just a JQuery to call a webservice and return values to be filled up on some other fields.
I found one working solution by putting a custom button as a field, but it is placed far from the field because its label (what seems weird for my purpose).
Let me know if this description is enough or additional information is needed.
example here
Tks.
I have been working around this problem. I have a user submitting a HTML form lets say to list their hotel on my website. I need to review this form before i add it into my hotels model to be published.
One approach i have worked on is to use a requests model where this form can be stored and later using django admin action write a custom action to add/remove the request. In case of acceptance i copy the details to my hotels model else it sends an email or a notification back to user.
Second approach is simply using django action on hotels model where the request is sent to approve it or reject it. In this case i want to know if thats possible where a data point doesn't get written to database until it's been accepted by admin. If yes how can i do that?
Finally, these details are displayed on my main page and search page for users to book these places.
If there is a better and effective way to do it. Please share that.
Thanks in advance. If something isn't clear i can answer your specific questions in the comments below 😊.
Have a nice day.
You can have is_published Boolean field in your hotel model and you can default it to false initially. After you inspect the hotel details you can set the is_published field to True from django admin.
So now whenever you are querying for hotels to show on your website. You can query
Hotel.objects.filter(is_published=True)
I read through the tutorial on the Django site for adding custom admin actions and got my custom action to work when viewing a list of items.
The problem is I'd like to add a button to the page where users can edit records that will execute this custom action, but I have no idea how to do this.
I am working on a site that would allow users to post some data. To successfully add a new post, the users need to go through three states: Form -> Preview -> Posted page. I want to restrict the users from going back to the Preview page with the browser "back button" once they have already reached the Posted page (instead, they should be redirected to the empty Form page). How can I implement this behaviour in Django?
I am not sure how you get this desired behavior from Django as you have limited control over the user's browser. However, in Javascript you can use:
window.location.replace(url);
which will remove history, thus preventing the back button from working.
See this stack overflow question about window location:
What's the difference between window.location= and window.location.replace()?
An idea: from your preview page, use AJAX to submit and if all is successful, window.location.replace to your posted page.
I can't speak for how to deal with this using browser technologies but with django you could just set a flag in the session.
# posted_page view
request.session['posted_page_visited'] = True
# preview_page view
if request.session.get('posted_page_visited'):
del request.session['posted_page_visited']
return http.HttpResponseRedirect("form_page")
Using js (window.location.replace(url)) doesn't fulfill this requirement because "replace url" will just replace the page with another one,Ex: if form flow goes from page1 to page2 then page3 then page4 and (window.location.replace(url)) is used in page2 (window.location.replace(page4);) then page3 will never be visited!! moreover user will still be able to go back in the same forward path meaning from page4 to page2...etc
the good thing, you can solve it by using Django session as shown below assuming users will be able to go back and forth as long as form not yet saved, and once its saved they can't go back anymore:
in page1/view function where first part of form is issued create session varaible:
in view1.py
def view1(request)
.
.
.
request.session['forward'] = True
return redirect(....)
in view2.py:
def view2(request):
if not request.session['forward']:
return redirect(..Select whatever page you want to redirect users to it..)
the same in rest of pages views..
in the last page/view where after saving the form, reset the variable:
request.session['forward'] = False
return redirect(..Select whatever page you want to redirect users to it..)
hopes its clear enough
django's form wizard should do what you want:
How it works
Here’s the basic workflow for how a user would use a wizard:
The user visits the first page of the wizard, fills in the form and submits it.
The server validates the data. If it’s invalid, the form is displayed again, with error messages. If it’s valid, the server saves
the current state of the wizard in the backend and redirects to the
next step.
Step 1 and 2 repeat, for every subsequent form in the wizard.
Once the user has submitted all the forms and all the data has been validated, the wizard processes the data – saving it to the
database, sending an email, or whatever the application needs to do.