Adding more fields to the signup form - python

I want to know if I can add more fields to the signup form if a user checks a box. For example, if the user checks the box that says he is a photographer one more field will appear in which he can specify what type of photography he does.

This is what DHTML is. You need to add an event on the checkbox which will dynamically add the INPUT element in the form. something like.
<input type="checkbox" onclick="addInput(this)" id="photography">
now in the method add a text element dynamically with dynamic id e.g.(value-photography) so that you can take an action on server side.
you need to make use of
document.appendChild()
You can use jQuery as well.

Related

Django template radio button auto select

I am developing an app with django. Here is the screenshot. I need to know when user will select on a plan in the next page that plan radio button should auto select.
I am able to grab all the information regarding the selected plan but dont understand how to select that plan's radio button.
here is the screenshots. Let me know anyone has the answer.
here is the next image where the radio buttons are present.
You could store the selected plan inside a sessions key and then check if that key exists in the next page... something like:
# in your first view set the session keys of posted plan
if 'express' in request.POST:
request.session['express'] = 'express chosen' # strings are easier to handle
# add the other plans to the if statement
# in the next page's view function, add keys to context if they are in the session framework like so
if 'express' in request.session:
express = request.session['express']
# add the other plans to the if statement
Now in the template, check if the variables exist to select option as default...
<input type="radio" {%if express%}checked{%endif%}>
<label>Express</label>
<!-- repeat for the other plans -->

Django dynamically add fields without creating new model instances

I'm attempting to create a form that allows someone to enter multiple phone numbers. I'd like to make it possible to click a button to add an additional field to the form but I am not quite sure how to do that.
I looked at FormSets but I don't want to create a new model instance every time someone adds a new phone number.
Thanks in advance
One way is, you could consider using an ArrayField or JSONField for the phone numbers. The latter might be more useful if you want to always be able to easily reference the numbers e.g
{'primary_no': '+123232323'}
Then for the frontend you can use a button with Javascript to 'clone' the input element (or better create a new one with the same class names) and append it to a phone number wrapper div on click. But you'll need to take note to use the same field name like one would for a checkbox element e.g
<input type="text" name="phone_number[]" />
This would make the values of these fields be sent in an array for request.POST.getlist('phone_number[]'), which you can then loop through and validate. I left out the nitty gritty details here but this should give you an idea.

I tried making a form of dynamic nature in django which change the number of fields in template as per user. screenshot of which is given below

I'm planning to make a dynamic forms in django which takes 3 basic fields one char type, one date type and one numeric integer value. I use basic django model forms and render method to save the form in database. But I'm unable to perform that operation in this dynamic form.
Any type of help will be appreciated.
You can do that in jquery/javascript in django template
Render simple form with all fields first see what are name of components id or name
then you show the first field and hide the other two on pageload
$("#idofcomponent2").hide()
$("#idofcomponent3").hide()
$("#idofcomponent1").on("focusout",function(){
//show second component here and hide the first
$(this).hide();
$("#idofcomponent2").show()
});
$("#idofcomponent2").on("focusout",function(){
//show second component here and hide the first
$(this).hide();
$("#idofcomponent3").show()
});
I was not able to test it. Please let me know.

Update database field with multiple buttons

I'm working on a small project using Django and I have a detail page of a digital product. The user should have the avability to update the status of this product through 3 Buttons. This status represents one ChoiceField in the database. The choices are: Dismiss, In Review and Approve.
Clicking the button should submit the form directly.
My question is, what is the best way to solve that (without Ajax)?
My first idea was to add 3 forms and every form has another action url.
I'm still fairly new to Django and don't know if my approach is the best solution.
As described in this question and in the docs you should be able to get what you want with a RadioSelect form widget.
If you add a bit of CSS to hide the radio inputs themselves, and style the labels as buttons, you should be good!
Edit to include my comment below:
To get a single click submission, you could write some simple javascript that catches the onclick event on your buttons, and submits the form as you click.

Performing sensitive Django actions while masking/hiding underlying URL

I have a simple Django view that confirms the deletion of an instance. On this page is a simple form containing two buttons, "Cancel" and "Delete."
The Cancel button simply returns the user to the page from which the original delete button was pressed.
The Delete button jumps to a second view that performs the actual action. Thus, my URLs are defined as follows:
url(r'^confirmDeleteItem/(?P<item_key>\w+)$', 'confirm_delete_item'), # Confirms
url(r'^deleteItem/(?P<item_key>\w+)$', 'delete_item'), # Performs the action
On the confirmation page, the form is defined with a POST action that visits the second URL:
<form action="/squash/deleteItem/{{ item.key }}/" method="POST">
...
</form>
The problem I have with this is that the Items are fairly large (they store lots of data) and sensitive, so I'd like to force the user to jump through the confirmation hoop every time.
I would like to either prevent the User from visiting the /deleteItem/ page manually, or just hide the browser's loading of this page to avoid from it becoming stored in the history, accidentally bookmarked, etc.
Is wrapping the action in an AJAX call the best way to solve this problem, or are there more standard/preferred solutions? Thanks!
How about setting a session variable in confirmDeleteItem view (i.e. prepareToDelete = item.key) and checking in deleteItem view whether this session variable exists and whether the value match the item.key? Then you'd just need to remove it after actual delete occurred.
A quick idea which I'm not sure of, but should work in your case.
Check your referer in your deleteItem view. Like in this snippet of code. If user didn't come to the delete view from confirmDelete view, redirect him to the appropriate confirmDelete view.

Categories

Resources