Need help populating a form from a set of templates

My parents’ small business of health & yoga was pretty much nuked by covid, so I’m now frantically trying to create a basic web app for them to be able to interact with their clients.

I wish someone could point me in the right direction or provide me with some ‘logic’ that can enable me to do the following:

When my mom is sending someone their weekly diet, I want her to be able to pre-populate the diet form from a few pre-saved diet templates, and I also want her to be able to save a given diet program as a ‘new template’, and name the template upon saving it - what would be the ideal way to do this?

Any help is greatly appreciated, thank you!

You can provide initial values in Django Forms.

Maybe you can save the templates in the DB, load them and pass the initial values to the form you end sending to the user.

Example from SO

def view(request):
    recipe = Recipe.objects.get(id=1)
    data = {'id': recipe.id, 'name': recipe.name}
    form = RecipeForm(initial=data)
    return render_to_response('my_template.html', {'form': form})

The “ideal” way is dependent to a large degree upon your vision for how this is going to work.

For example, my first reaction to what you’re looking for would be to create the form for the diet, and then add a widget to “Load diet from template”, with a drop-down with the list of available templates. I’d then have some JavaScript that, when a template is selected, call an API in my project to retrieve that template and use the data returned to populate the diet form.
I’d also add a field for a template name, and then have either two or three buttons to “Save”, “Save as template”, and if necessary, “Save as both”.

But that’s just one idea. If you want to avoid JavaScript, you could come up with a different workflow.

So I think “ideal” here is going to be whatever you’re comfortable doing and what your parents are going to find easiest to use.

1 Like

Thank you for your response, it gave me some clarity regarding this matter.

Another thing I have realised researching this for the past day is that this would be done not on the backend but rather the frontend, I was getting confused over nothing!

Hello,

I am trying to achieve pretty much what the original question was about, the main difference being that I need to do it in the admin interface (because my app is supposed to be a backend so I only implemented the admin views). Which means I do not deal with JavaScript (yet).

What kind of workflow would you advise me to implement ?

(I am new to this forum, if this is not the right place to ask this question, please kindly show me the way)

Do not mistake the admin as being intended as the base for a user-facing UI - even when there’s only one user. It’s not intended to be your general UI.

A custom workflow, with a set of views - possibly using the Django FormWizard tool to walk through the process in multiple steps.

Notwithstanding the above, you can create a custom form to be used by the admin, and the admin site already makes jQuery available which means you could implement custom functionality within your form.

Thanks for your immediate response !

Do not mistake the admin as being intended as the base for a user-facing UI - even when there’s only one user. It’s not intended to be your general UI.

The admin’s recommended use is limited to an organization’s internal management tool. It’s not intended for building your entire front end around.

It is supposed to be an internal management tool rather a solid front interface. So I thought it was okay to use only the admin interface as is written in the link you provided. My app is mainly a database for which I provide routes that I request from my already existing front end applications.

A custom workflow, with a set of views - possibly using the Django FormWizard tool to walk through the process in multiple steps.

This looks like it could help me solve my issue, I’ll take a look at it right now.

Notwithstanding the above, you can create a custom form to be used by the admin, and the admin site already makes jQuery available which means you could implement custom functionality within your form.

I was looking at the custom forms when I asked the question, but I had not yet found how to use JQuery within them. I’ll take a look at the second link afterwards.

Thanks again :slight_smile:

PS: I don’t know how you linked the whole question while showing only parts of it.

Sure - it’s ok. But it’s not intended to be a full UI. As the referenced text goes on to say in the second paragraph:

If you need to provide a more process-centric interface that abstracts away the implementation details of database tables and fields, then it’s probably time to write your own views.

And that’s the territory you’re starting to wander into here.

What I (and others) have seen, are people building their application around the admin UI, and quickly running into the limitations of what the admin classes can do. So they spend a lot of time & effort writing code to work around those limitations, when their requirements could have been much more simply implemented by writing a couple of custom views.
It’s a rabbit hole that’s very easy to dive into and much more difficult to climb out from.

To be honest I am kind of afraid of falling into that trap.