[Python] How can I style a django form with css?

Discussão em 'Python' iniciado por Stack, Março 15, 2025.

  1. Stack

    Stack Membro Participativo

    I tried looking for the answer earlier but couldn't seem to figure out a few things. I'm creating my form in a form.py file so its a python file.

    Here is my forms.py file :

    class UploadForm(ModelForm):
    name = forms.TextInput(attrs={'class': 'myfieldclass'})
    details = forms.TextInput()
    littype = forms.TextInput()
    image = forms.ImageField()
    class Meta:
    model = info
    fields = ["name", "details", "littype", "image"]


    Here is my views.py function for it if it helps find the solution :

    def uploadform(request):
    if request.method == 'POST':
    form = UploadForm(request.POST, request.FILES)
    print(request.FILES)
    if form.is_valid():
    form.save()
    redirect(home)
    return render(request, 'uploadform.html', {'form': UploadForm})


    To style it I thought I could do something like this which I found in another question :

    class MyForm(forms.Form):
    myfield = forms.CharField(widget=forms.TextInput(attrs={'class': 'myfieldclass'}))


    Except I have no idea how to link a css page to that python file. This is what I tried writing but i think it doesnt work because its meant for html but its in a python file :

    <link type="text/css" rel="stylesheet" href="templates/form.css">


    And so I'm just not sure how to style my form. Thanks for any answers!

    Continue reading...

Compartilhe esta Página