django forms placeholder not working as expected?

43 Views Asked by At

I created a django form and tried to add placeholder and remove the form label. i came across a solution to add the palceholder but it seems to have no effect.

this is screenchot of that form

This is forms.py

class AppForm(forms.ModelForm):
    class Meta:
        model = App
        fields = ('__all__')

        def __init__(self, *args, **kwargs):
            super(AppForm, self).__init__(*args, **kwargs)

            self.fields['appname'].widget.attrs['placeholder'] = 'App Name'
            self.fields['link'].widget.attrs['placeholder'] = 'App Link'

This is my html file

{% extends 'main/base.html' %}
{% load static %}
{% load crispy_forms_tags %}
{% block content %}

<div class="content-above">  </div>
<div class="content">
    <div>
    <form action="" method="post" autocomplete="off" enctype="multipart/form-data">
        <div>
        {% csrf_token %}
        {{ form.appname|as_crispy_field }}
        {{ form.link|as_crispy_field }}
        </div>
        <div class="row">
            <div class="col-md-8">
                <button type="submit">Submit</button>
            </div>
            <div class="col-md-4">
                <a href="{% url 'app_list' %}">Back to list</a>
        </div>
    </form>
</div>
</div>
{% endblock content %}

Please help and if possible also tell me how to remove the default label!

1

There are 1 best solutions below

0
rohit10 On

so all I had to do was get the indentation right

new

class AppForm(forms.ModelForm):
    class Meta:
        model = App
        fields = ('__all__')

    def __init__(self, *args, **kwargs):
        super(AppForm, self).__init__(*args, **kwargs
        self.fields['appname'].widget.attrs['placeholder'] = 'App Name'
            self.fields['link'].widget.attrs['placeholder'] = 'App Link'

old

class AppForm(forms.ModelForm):
    class Meta:
        model = App
        fields = ('__all__')

        def __init__(self, *args, **kwargs):
            super(AppForm, self).__init__(*args, **kwargs)

            self.fields['appname'].widget.attrs['placeholder'] = 'App Name'
            self.fields['link'].widget.attrs['placeholder'] = 'App Link'