Advanced forms with django-form-builder - django

So, I've been working with the django-forms-builder library. Now, the use case I'm working on says, that using the admin interface, once creates a form asking for details such as the user's name, email, qualifications and so on. Now, the thing is - a user could have multiple qualifications, so the requirement is to display one field, as well as a small "plus" button - clicking that button displays another field, where you could add a second qualification.
Django-forms-builder does everything else I need it to - but this is the only part where I'm stuck. To make matters worse, I'm not a full-blown Django programmer. Would be grateful for any pointers I could get.
EDIT - To clarify, in my project, one defines the form that will be shown to the end user through the Django admin interface. The form will then be displayed to the end user, and if the end user has any additional qualifications s/he'd would like to add, the form that would be rendered, would have that plus button, to add another field.

Related

Is it possible to make 'cart' with django Class Based View?

I am trying to make a simple e-commerce website and followed some tutorials.
However, the author of the book used complicated function based view to make cart function..
there are bunch of session stuffs.. and I don't understand the logic..
and I am trying to think the other way..
what about using database to store all the cart related data, and
use CBV to build it?
for example,
CartListView to see the contents of the cart, and CartUpdateView to change the quantity..
then are they going to be two different pages? separated page that user should go to the
different page to change the value??
please help me T T
You can access the session in any sort of CBV as self.request.session and a "shopping cart" is normally stored therein.
You'll certainly need to implement a CartListView to see what's in it, or possibly a CartEditView to show the cart with options to edit the quantities and delete anything that shouldn't be in there.
Adding products to the cart may well be an "Add" button on a ProductDetailView or lots of add buttons in a ProductListView. You might add a POST handler method to these views which are otherwise read-only (GET-only) bt default. Or you might make them FormViews, even though the form would be hidden and filled/POSTed by JS rather than the shopper doing anything other than clicking "add".
And then there will be a CheckoutView.
Check https://djangopackages.org/ (put "cart" in the search box). this will throw up several shopping cart things which might be the code you want, or the source of which might be a valuable learning resource before you end up rolling your own.

Django Forms: How to add icons instead or in addition to the form fields

Given that we live in the emjoi era, you may want to have a form to ask for user input using icons. Say for example, you want to find out which sport the user prefers. Instead of or in addition to the text, you may want to display the icon for the choice (e.g. showing a basketball for basketball choice).
How do you go about this in Django in an automated way. I understand that I can manually render each field and add HTML to it, but that's more hacking your way through. What if I want to render so many fields in a formset with so many choices for each field of each form using a for loop in the template and do not look forward to manually going through the fields and add the icon for each field of each form.
This awesome (pun intended) package does it all. And you can build on it's IconField if you need different icons.

django model attribute edit & save with no extra page

I rende certain objects values in django template in a form of a table.
I let user to edit the value and save the edit so I can track the history of edit.
At the moment I use django forms to let user do single object attribute value OR chosen objects attribute values OR all of them and save it.
My problem is with forms is that the way it works at the moment is:
user clicks a value in 'main' page so it links to object 'edit' page in which I return a form so user can edit it.
The problem is with that extra url or extra page. I do not want to do it via separate pages.
I would like to click on the object (like in the excel) and change the value there in 'main' page and submit the edits from the same page.
How can I achieve it with django ?
Can somebody point me into right direction and point out what I should read about to understand it or how I should do it ?
I want to edit either single or let user edit multiple objects values and save the changes and still be able to track the history of edits / changes.
You should look into modals. So when a user clicks to edit it will display a pop-up that you can render the form in without leaving the page.

Repeating forms in django form wizard

I've created a form wizard in django which starts with a form which the user can enter their details (name, age etc.) into.
The wizard then goes on to some other forms I need.
Now I want to expand the wizard so that the user can add as many other user details as they like.
So basically, it needs to be something like:-
User details -> Do you want to add more?
If yes -> User details
If no -> Next form
I also need the user to be able to edit previous user details or remove previous user details entirely.
I've tried adding a load of conditional forms for the user details, which are switched on or off, depending on whether the user replied Yes to the "add more" question. However, all of the user details form fields have the same name so they overwrite each other. Also, this seems like a hack to me.
So what's the "proper" way to do this? Put simply, how do I conditionally repeat forms based on user input as the wizard progresses?
Then you can use django formsets https://docs.djangoproject.com/en/1.9/topics/forms/formsets/ that way the user can add as many as they like.. However I am not sure how that works with the Wizard thought because of the management form.. You can try it and return feedback :)

Should I modify/extend the admin interface, or write my own CRUD views/templates?

I'm trying to write a simple CRM app in Django; partly as a learning exercise and partly for in-house use.
My schema is slightly complex, as rather that have a single Contact model (with a home phone, work phone, home email, etc.), I have stripped down Cntact model plus a Phone model, an Email model, etc., with a ForeignKey pointing back to a Contact. The point is to let Contacts have an arbitrary number of phone numbers, email addresses, etc. Simple, right?
I have some working views and templates for displaying the data - no issues there. And with only a very small amount of poking at admin.py I have a um...eight different TabularInlines set up, and the admin interface works to create and edit the data...but it's ugly and clunky to the point of unusability, and of course there's no conception of permissions or anything. I'm also not really a fan of having a completely different interface for displaying and searching through the data than for editing and adding contacts...I'd like as much as possible to be done inline, so that I can search for a name, look at the record, click "add note", have it popup a form, fill in the details, click submit, and be done, all with AJAXy goodness so there's no page reloads.
Question: Should I plug away at modifying the admin interface to try and make it usable for a user-facing app? And if so, can anyone point me to a good guide or example where someone has really changed the admin interface to make it work for user-facing CRUD operations?
Or should I just go ahead and write my own CRUD views? And if so, can anyone point me to a good guide or example where someone has written custom CRUD views that work with lots of ForeignKeys and inlines? Ideally I want a form that displays a single Contact, all his Email records, plus a blank form to add a new Email record, plus a button to add more blank forms, plus his Phone records, plus a blank form, and so on for all 8 of my associated models.
(Or am I thinking about this all wrong? Any advice appreciated.)
For our intranet, we use ModelAdmin subclasses (not mounted on the admin site via admin.site.register) for most of our C(R)UD views. By using custom templates for the views, it doesn't look like Django admin at all. What is very convenient though, is that it already handles all the validation/saving for us.
In general, I found admin-"hacking" quite useful to quickly write up C(R)UD views and usually with relatively small changes to your ModelAdmin subclass, you can make it work for your use case.
So I'd vote for use ModelAdmin, but not the one you use in admin, hook a different template and come up with some fancy CSS.
I successfully created a software on top of admin.
The admin hooks (these days) allow very fine-grained customizations, i.e. in general you only need to touch what you want to change.
The changes can go from a trivial cosmetic adjustment to a complete swap-out:
If you provide templates/admin/base.html your admin site can look any way you like. And of course, a navigation bar at the top could include links to some of your own views. Watch out not to hardcode URLs in your links, always reverse.
You can overload ModelAdmin's "change_view", "changelist_view" etc. and swap them for your own views. For example I replaced a default changelist and its simple filtering with a search interface that allows dynamic queries to be built, result columns to be customized by the user, and loading/saving of these searches. That didn't affect any of the other views of that ModelAdmin.
Overloading a ModelAdmin's "get_urls()" let's you rewrap existing admin urls to go to your own views. I did the latter for one model where I wanted the simple Add screen to be replaced by a totally customized Wizard (only leaning on ModelForm).
Don't forget the simplest approach, esp. regarding your "AJAXy goodness": Just define "css" and "js" in your ModelAdmin's Meta. Want to move an inline from the bottom to sit between third and fourth field, and that's not possible via parameters? A one-liner in jquery.
Check out "django-grappelli" for an example of how to improve admin look and feel.
What did you mean by "and of course there's no conception of permissions or anything"?