Add custom button to form

I’m trying to add a custom button to my model form. This button should redirect user to another website. I’ve tried to do

class FootballMapAdminForm(forms.ModelForm):
    extra_link = forms.URLField(
        label="Some Label",
        widget=forms.URLInput(attrs={'style': "margin-bottom: 40px", "type": "button"}),
    )

    def __init__(self, *args, **kwargs):
        super(FootballMapAdminForm, self).__init__(*args, **kwargs)
        self.fields["extra_link"].initial = "https://example.com"

but it doesn’t do any redirect action on click

add button. in form html not forms.py

<form>
{{ form }}
</form>
<button>add your custom button</button>

why not use a link? <a href="...">my button</a>

How can I do this in the forms?

in the template?

{{ form }}
<a href="..." target="_blank">Your link to somewhere else</a>

No, in the forms, as I asked from the beginning

a form cannot handle an anchor, as far as I know… a form handles form element such as input, textarea, etc.

you seem extremely reluctant to use the template… may I ask why?

I know it doesn’t work.
Forms are for creating input fields, not buttons.

You want to add the onclick attribute to the button widget. This attribute will trigger a JavaScript function that redirects the user to a specified URL (https://example.com in this case) when the button is clicked. Am I right?

from django import forms
from django.utils.safestring import mark_safe

class FootballMapAdminForm(forms.ModelForm):
    extra_link = forms.URLField(
        label="Some Label",
        widget=forms.URLInput(attrs={'style': "margin-bottom: 40px", "type": "button", "onclick": "window.location.href='https://example.com'"}),
    )

    def __init__(self, *args, **kwargs):
        super(FootballMapAdminForm, self).__init__(*args, **kwargs)
        self.fields["extra_link"].initial = "https://example.com"