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.
Related
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.
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.
I am new to Django and I am right now trying to deal with the issue of filtering based on specific field. React.js is used for the front-end and Django is used as RESTful APIs.
For the front-end, users are allowed to choose to either search based on number or name. The info has been successfully collected. The following code just for better understanding and is incomplete.
<input type="text" /><label>Search by number</label>
<input type="text" /><label>Search by name</label>
For the backend, response should filter the queryset based on input information, which is either number or name. However, when I went through the document, I noticed that for filtering, all the filtering fields should be provided.
Entry.objects.filter(number=123, name="abc")
How can I do the filter based on the given info from the front-end, for example, if user chooses to search by name that Django does:
Entry.objects.filter(number=123)
while if user chooses to search by name, then Django does:
Entry.objects.filter(name="abc")
I understand that I can actually create two different APIs endpoints, so the number and name won't interfere each other. However, if there are more filter fields, then it is definitely inefficient to write every single API based on each field.
Anyone can help me with this? Please let me know if I explain it clearly or more info should be provided.
You can use Q objects to filter results with oring
Something like below
from django.db.models import Q
Entry.objects.filter(Q(number=123) | Q(name="abc"))
There are more ways to use Q objects refer the docs.
I want to display multiple instance of same object in a form and collect all the data in single click. One example I can think of is let say, I want to take attendance of fifty students in my db. I need to present the user with all the students and a check box so as to mark, who all are present and submit the result at end.
Note: I am fairly new to Django, I am okay with models and forms as a beginner.
I'm wondering what the best approach to take here is. I've got a form that people use to register for a class and a lot of times the manager of a company will register multiple people for the class at the same time. Presently, they'd have to go through the registration process multiple times and resubmit the form once for every person they want to register.
What I want to do is give the user a form that has a single <input/> for one person to register with, along with all the other fields they'll need to fill out (Email, phone number, etc); if they want to add more people, they'll be able to press a button and a new <input/> will be generated. This part I know how to do, but I'm including it to best describe what I'm aiming to do.
The part I don't know how to approach is processing that data the form submits, I need some way of making a new row in the Registrant table for every <input/> that's added and include the same contact information (phone, email, etc) as the first row with that row. For the record, I'm using the Django framework for my back-end code.
What's the best approach here? Should it just POST the form x times for x people, or is there a less "brute force" way of handling this?
Django includes FormSet for dealing with exactly these challenges. Using a FormSet you can create multiple forms for creating or updating information. There's even possible to generate the FormSets from a Model. http://docs.djangoproject.com/en/dev/topics/forms/formsets/ and http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#id1 are great resources.
Now, for creating more forms on the fly, you need some javascript magic. I've done this on work projects using jQuery which made it a lot simpler. The basic idea is create a new form with the correct inputs and change the hidden metadata in the formset form so it will now how many forms to process. The admin implements this when using multiple inline forms so I suggest looking there for code as it is a bit tricky to get right.