Can a user create forms in django? - python

I'm officially stuck. I'm am building a django application for work purposes. Staff users should be able to create forms using the django admin. The forms should the be able to display on the website with variable labels. Check link for html setup. The labels above the inputs should be editable from the django admin for each specific form.
I would also like for users to be able to create as many fields as they like. So for example in the image in the link, there are 8 input fields. Is there any way to do this with a django form/model? I looked at the django forms-builder extension. Basically I want the functionality of being able to press "add another field", but with everything else that comes with the forms-builder package.
The form should be stored in the database attached to the corresponding label of that field.
I've created my own abstract user, and one user should only be able to fill out a particular form once. But should be able to fill out different forms though.
One form should be able to be filled out by many users.
Every idea is much appreciated!

django-forms-builder might actually be a good fit for your case. This module provides an ability to modify forms and add fields from the admin interface.
Regarding your constraint with allowing a particular user to submit form only once, there are at least two ways to do it:
First and obvious one would be simply to check that condition in a view, before showing the actual form to a user. You can use request.user instance to check if his username is already in a list of respondents to this form.
Another possible solution would be to use Signals that are provided with django-forms-builder module. They allow you to catch form submission moment either when the form data is valid or not. You can add your custom logic there, e.g. raise alert if a user has already submitted this form before.

Related

Let user add fields to form WTForms

I'm working on a basic web form using python Flask and the Flask-WTForms extension. I don't know how many input fields a given user is likely to need, and so I want to implement user driven addition of fields. Something like a user clicks a button and another field shows up in the form. I've done some searching on my own but all I could find for WTForms was dynamic addition of fields, which isn't quite what I want to be doing.
Is this possible/practical with WTForms? Would I be better off trying to hack it in with JavaScript on the front end? Or should I just give up on this idea?

How to storage objects for communication without the database in Django?

I'm working on a project where i do need to do a communication in 3 parts. Let me explain:
1.- The user fills a form, which is used to create a set of objects from the models. This objects are not saved in the database on this moment. All the objects are related between them.
2.- This objects must be saved in some way for an admin user to check the data and decide if proceed to save it, or if any element is invalid and reject the form.
3.- If the admin decide that the data is correct, select an option to send a .save() to add the data to the database.
At least that's the idea i have on how should work. I decided to create the objects before send it to the admin because it sounded easier to show that if i sended the request.POST and the request.FILES. My problem is that i don't know where could save the objects to show it to the admin when he connects(There are 2 types of users, Normal and Admin, the normal are the ones that fill the forms, the admins only check it, the two of them has their own views). So, does anyone how could i send the data and storage it until the admin connects? I'm open to any idea if this is not possible, the only thing that is necesary is the flow of: User fills the form, admin check it, data is saved or rejected.
You will want to save() them when the user submits the form at the start. Add a BooleanField to your model that says whether the row has been moderated and accepted. Then in your application, filter out all non-moderated rows, and on the admin side, filter out only rows that need moderation.

Split Sonata User Bundle registration form into stages

I'm currently working on a user registration form for a site I'm working on. The site requires a lot of information about users up front, and I'd like to break it down into stages.
The first stage requires the user to put in an identifier in the form of an order number. I would then check the order number exists in the system before making them continue to fill in the rest of the fields. I'm not sure how to go about doing this.
What I've managed so far though is to override the underlying User, RegistrationController and RegistrationFormType and render out a customised view with the relevant form fields.
An overview of the process I'd like would be:
FORM: ask for order number
process form and check order number exists (if not go back to 1)
FORM: ask for user information
process user information and store
complete
Simplest way of doing it will be to override registration template, and hide all fields except those you wanna show in first.
And than add some js validation rules, - to show up parts of form based on values prefilled by user. In that case you do not need to rework Sonata registration form, but for user experience it will be the same.

Web2py form field options

I am using web2py forms and i want to have some fields only visible to user (as fixed which cannot be edited). I tried making various combinations of editable, writeable, readonly but was of no use. I looked into web2py book too but that also seems insufficient. It would be great if someone can tell me how to do this.
You mean some fields visible to all visitors and some fields visible only if logged in?
If that's the case, then build your form conditionally:
form_fields = [
Field('pubfield'),
Field('pubfield2')
]
if auth.user: # This is true if the end-user is logged in and you're using the built-in auth
form_fields.append(Field('private_field'))
return dict(form=FORM(form_fields))
Unless you're not talking about logged in users, and just want to make the fields be visible, but not editable. Then, use writable=False like you tried, but I think you have to either use crud.create/crud.update or SQLFORM / SQLFORM.factory (the latter does not require a data model)
SQLFORM.factory(Field('my_readable_field', writable=False))
If you the form is based off of a database, you can use CRUD (you'll need to modify the settings for CRUD if you're not using authentication, so that CRUD forms are accessible)
crud.create(db.some_table)
or
SQLFORM(db.some_table)

Remove the “Add” functionality in Django admin [duplicate]

This question already has an answer here:
Django Admin - Disable the 'Add' action for a specific model
3 answers
Is there a way to remove the "Add" functionality on the Django admin site? For certain entities, I only want the Django admin to be able to view them or change existing ones, but not add new ones.
Sure, you can customize admin VERY granularly by following the instructions here -- I believe that what you want can be obtained in part by overriding ModelAdmin.save_model(self, request, obj, form, change) in your own ModelAdmin subclass, to ensure nothing happens on the store when change is false (i.e. an attempt to add rather than change), and in part by overriding ModelAdmin.add_view(self, request, form_url='', extra_context=None) to display an "add view" that makes it very clear to the admin that they're NOT going to be allowed to add object through this route. I haven't actually done the specific admin customization you require, but I've done others and they do seem to work pretty smoothly!
See: Django Admin - Disable the 'Add' action for a specific model for true solution.
You can customize the permission for each user group from within the admin interface: try going to /admin/auth/group and it should be straightforward from there.
This won't be as granular as the solution offered by the earlier answer, but it will take care of most of your needs without needing to customize the admin.
If you change the permissions to restrict access then you'll still get the plus sign by a FK/MtM field. Clicking that will open a popup window with 'Permission Denied' in it.
You can actually completely remove the plus sign by not simply not registering the model with the admin.
I have a situation where I have predefined categories that I want users to be able to select more than one of. The best way to do this is with a models.ManyToMany field. You can register the model with the admin, enter the data as required and then remove the registration.
An easy effective way is to set max_num=0 for that particular inline.
Satya's suggestion of setting max_num=0 works perfectly.
Per the Django docs on the ModelForm class:
For users with JavaScript-enabled browsers, an "Add another" link is provided to enable any number of additional inlines to be added in addition to those provided as a result of the extra argument.
The dynamic link will not appear if the number of currently displayed forms exceeds max_num, or if the user does not have JavaScript enabled.
and
As with regular formsets, you can use the max_num and extra parameters to modelformset_factory to limit the number of extra forms displayed.
max_num does not prevent existing objects from being displayed

Resources