Creating code from Template

I’d like to create a project to generate a certificate (like a diploma for example) from a web formular in which the user would fill in his personal details. The goal is to let the user select different certificates to genrerate: sober, Colourfull, black&white, etc …

My question is : how from Django also use the template to generate the Latex code for the different buttons relied to certificate styles ?

Of course the Latex code is for example, I just need to know how from a view a template could be filled with the user details only so it would be saved as a static file

I’m not sure I have understood your requirements concerning the buttons to select a style, but here’s what I’d do:

  • A view the user accesses that renders a form, into which the user can enter all the details necessary to create a document. There’s also a select element for the user to select the style of the document. If different styles require different inputs, the form contains all possible inputs and a JS script will activate only the inputs required for the current style (although there are other solutions for this).
  • When the user submits the form, the view recognizes that POST data exists and selects a template corresponding to the selected style. A template can be anything as long as the document’s syntax does not clash with the syntax of the template language. If it does, the conflicting elements have to be escaped with a neutral character and then unescaped after the template has been rendered.
  • The view can then do whatever with the rendered template. Maybe save it as file or do further post-processing or maybe just send it back to the user

You’ll find most the information you need starting with the Django Templates documentation page.

You might also want to read more about Templates.

You may find the render_to_string function useful. (Once you have a rendered template as a string, you can do anything you want with that string (e.g. write it to a file).

Thank you for your answer which gives me some interesting clues.
Could you specify a little bit what you meant by

When the user submits the form, the view recognizes that POST data exists and selects a template corresponding to the selected style

thanks Ken render_to_string is what I didn’t found yet

I’m refering to a standard setup of a view with a form. That in of itself requires to handle POST data, populate the form, render the view, etc. Now in the case that there is POST data, the user has filled out the form and submitted it. In addition to the normal form handling the view then also looks at the value of a select element of the form that corresponds to a selection the user made as to which template to use.
This might look something like this (snippets only, this is not complete/good code):

style_choices = (
    ("style_1", "Style 1"),
    ("style_2", "Style 2"),
)
class StyleSelectForm(forms.Form):
    style = forms.ChoiceField("Select a style", choices=style_choices)
    name = forms.CharField("Your name", required=True)

def style_select(request):
    if request.method == "POST":
        form = StyleSelectForm(request.POST)
        if form.is_valid():
            context = {"name": form.cleaned_data["name"]}
            if form.cleaned_data["style_select"] == "style_1":
                document = render_to_string("/path/to/templates/style_1.tex", context)
            elif [...]
    [...]
    

Great, I have not already found such way to modify the view process and instead to the render_to_string then to a file which is so easy with python.

Thank you