How do I connect a form whcih is written in HTML with My class in model.py?

I want web users to input specific words to the form on my website, and I want to let my class or function in model.py process the input data to generate a picture. Then implement the generated picture on the result page shown after pushing the submit button.

There are 2 questions about this.
How do I connect Form and my function?
and how do I implement png from a directry to html?

if you help me in detail, that is very nice.
You can simply throw related documents, URL, and the name of importable things or library at me. That might largely help me out as well.
thank you very much.

Hey there - for this kind of basic question, you would be best following the Django tutorial or the DjangoGirls tutorial - they will teach about forms and how to connect them to functions.

Then, to handle pictures, you will have to understand how to have Django send non-text responses - you’ll need to pass raw binary data and a custom mimetype to Response, and then you can send anything you like. I recommend you try with a fixed picture first before you generate one.

If you have specific questions, I’m happy to try and help, but your initial open-ended question is too large for any one person to help without giving full instruction!

(You can find those tutorials at : https://docs.djangoproject.com/en/3.0/intro/ and https://tutorial.djangogirls.org/en/ respectively)

Thank you for helping, those replies made me able to convince myself of that my plan was actually feasible.

Create a Django Model: First, define a Django model in your models.py that represents the data you want to collect from the form and process. For example:

from django.db import models

class InputData(models.Model):
    input_text = models.CharField(max_length=100)
    # Add other fields as needed

Create a Form: Create a Django form in your forms.py that is associated with the model you created. This form will handle the validation and processing of the user input.

from django import forms
from .models import InputData

class InputDataForm(forms.ModelForm):
    class Meta:
        model = InputData
        fields = ['input_text']

Create the HTML Form: In your HTML template, create a form that matches the fields defined in your form class. Make sure to include the CSRF token and specify the form method as “POST.”

<form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Submit</button>
</form>

Handle Form Submission: In your Django view function, handle the form submission, validate the input, and process it using your model or any other logic.

from django.shortcuts import render, redirect
from .forms import InputDataForm

def process_input(request):
    if request.method == 'POST':
        form = InputDataForm(request.POST)
        if form.is_valid():
            input_data = form.save()
            # Process input_data or generate the picture here
            return redirect('result_page')  # Redirect to the result page
    else:
        form = InputDataForm()
    return render(request, 'input_form.html', {'form': form})

Display the Result: Create a view and template for the result page where you can display the generated picture or any other processed data.

Serve the Picture: To serve a PNG image in Django, you can use the HttpResponse with the appropriate content type. For example:

from django.http import HttpResponse

def serve_image(request):
    # Generate your PNG image or retrieve it from a directory
    image_data = open('path/to/your/image.png', 'rb').read()
    return HttpResponse(image_data, content_type='image/png')
URL Configuration: Update your urls.py to map the views to URLs, so users can access the input form and result page.
from django.urls import path
from . import views

urlpatterns = [
    path('input/', views.process_input, name='input_form'),
    path('result/', views.result_view, name='result_page'),
    path('image/', views.serve_image, name='image_view'),
]