Can I generate a form depending on the results of a view? - drupal-8

I need to create a form which depends on the results of a view. I can generate it within the view and/or create a view template in order to do this....But how do I process it?
How do I connect my form/template generated form to a form_submission function?
When this custom/dynamic form is submitted I need to call some drupal functions to create some content in the site.

I did not find any way to do it in a view, but what I did was:
- Create a form programatically, making only a hidden field for NIDS
- Place my view next to that form using AJAX
- When the view is updated I copy the results of the view to the NIDS field and submit the form (This reloads the page)
- Form submission has 2 behaviours, 1 to re-generate the form when provided with NIDs, and other one for the actual form created by the NIDS result of my view.
Just in case this helps someone

Related

How to pass context object from a template to a view in django?

In search results page,a list of experts with their name and some other details are displayed.The name and the details are accessed from a context object "expert" that has been passed to search results page.Beside each search result I also generated a "save" button .When the user clicks on the save button,I need to save the individual expert data to a database.How do I send this "expert" context object from the search results page to a view so that I can then save it to the database?
Here is the search result page:
Here is part of the code of the for the search results:
You can see I tried to create a hidden form with a button to send the context object to a view but it's not working.How do I create the button so that when the user clicks "save" button the url sends the context "expert" to a view?
I tried to create a hidden form with a button to send the context object to a view but it's not working

Add an additional form to a formset using POST without Javascript and validation

I want to use a Django formset, but with pure server rendering and without the need of Javascript to add additional forms to it. The user should just click a button on the page and the page should reload with an additional form in the formset. All user input should be preserved! The relevant part in the view is:
if request.POST.get('add_form') == "true":
cp = request.POST.copy()
cp['form-TOTAL_FORMS'] = int(cp['form-TOTAL_FORMS']) + 1
fs = MyFormSet(cp)
The problem is that when MyFormSet(cp) renders a form representation it adds validation errors to it (like "This field is required"). This is ugly and not acceptable. How can I render it without the errors (they should only be present when the whole form was submitted)?
MyFormSet(initial=...) seems not to be an option as it must also work in a UpdateView (the docs are pretty clear that initial is only for extra forms) and also the POST data can't be directly used as initial values.
I am super thankful for any hint as it took me several hours without getting anywhere (and it seems to be such a common feature as the rest of Django is so Javascript agnostic).
It feels like a hack, but this works (after the formset was initialized):
fs._errors = {}
for form in fs:
form._errors = {}
This removes all errors from the fields when it is rendered to HTML. The background is that when _errors is set to None (the default) the form validates itself when it is rendered. When it is set to an empty dict the form will not validate itself anymore and just "thinks" that there are no errors in it. So no error messages are rendered.

Form of a list of instances

I am missing an idea or a way to handle this in django.
I have a list of instances (class Sample) and I want to present the last 10 of them in a form with a checkbox before... I want to have a button "work on these" to submit the checked instances to a view. This view should present a detailView of the selected instances.
I don't know, how to handle this exactly. Can somebody give me a hint? All I did so far was just creating a form of a instance.
Thanks in advance.
You can use a ModelChoiceField or a ModelMultipleChoiceField.
class MyForm(forms.Form):
my_list_of_instances = forms.ModelMultipleChoiceField(queryset = MyModel.objects.all().order_by('-id')[:10], widget=forms.CheckboxSelectMultiple)
This should generate a list of checkbox with your last 10 instances created.
With this you have your form ready to receive your input...just point your action to your view.
Edit:
In case you want to have several forms, one for each instance, you can work with Formsets. You would not get the checkboxes tho...you would get any change applied to your instances. If you want to have both (checkboxes and formsets) you can use both suggestions, using the checkbox values sent to the server to filter the formsets you are going to save.

'Hiding' form query from URL (Django 1.3)

I have a form with 6-7 fields. After user input, my webapp searches for those fields in a database and displays the results.
Now the issue is, that the URL ends up having all the form field names and their values in it.
result/?name=lorem&class=arc&course=ipsum
Now with the form having 7-8 fields the url ends up looking ugly.
Is there a Django technique to 'hide' these from the URL? Quotes around hide because I'd be okay with a completely different way to pass the objects to my database from the form as well.
Use a POST request. Here's the django docs on forms and a specific example using POST>. HTML-wise, all you need to do is change the method on the form tag.
I do not recommend to use POST requests for search. If you'll use GET it will be easer for user, he can just bookmark a link and save search or share search results with friends.

How to make fields readonly while updating

I have a form. Once the form is filled I don't want the user to change anything in the form.
But the user can see the values. Meaning all the fields are non editable. I can do this by using instance method but this does not help in foreignkey.
Depends on what you mean "once the form is filled".
If it's an html form post, just render a new html page with simple text and values of the submitted form.
If the post-back was an ajax call, you can change the CSS-styling of the elements for example and disable the submit button or erase the whole and substitute the values that you get back from ajax request.
There is no "editable=False" property on html input elements btw.
you can use readonlyAdmin