CSS styling in Django forms

Taken from my answer to: How to markup form fields with <div class=’field_type’> in Django class MyForm(forms.Form): myfield = forms.CharField(widget=forms.TextInput(attrs={‘class’: ‘myfieldclass’})) or class MyForm(forms.ModelForm): class Meta: model = MyModel def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) self.fields[‘myfield’].widget.attrs.update({‘class’: ‘myfieldclass’}) or class MyForm(forms.ModelForm): class Meta: model = MyModel widgets = { ‘myfield’: forms.TextInput(attrs={‘class’: ‘myfieldclass’}), } — EDIT … Read more

What’s the best way to store a phone number in Django models?

You might actually look into the internationally standardized format E.164, recommended by Twilio for example (who have a service and an API for sending SMS or phone-calls via REST requests). This is likely to be the most universal way to store phone numbers, in particular if you have international numbers work with. Phone by PhoneNumberField … Read more

How do I add a placeholder on a CharField in Django?

Look at the widgets documentation. Basically it would look like: q = forms.CharField(label=”search”, widget=forms.TextInput(attrs={‘placeholder’: ‘Search’})) More writing, yes, but the separation allows for better abstraction of more complicated cases. You can also declare a widgets attribute containing a <field name> => <widget instance> mapping directly on the Meta of your ModelForm sub-class.

How do I filter ForeignKey choices in a Django ModelForm?

ForeignKey is represented by django.forms.ModelChoiceField, which is a ChoiceField whose choices are a model QuerySet. See the reference for ModelChoiceField. So, provide a QuerySet to the field’s queryset attribute. Depends on how your form is built. If you build an explicit form, you’ll have fields named directly. form.rate.queryset = Rate.objects.filter(company_id=the_company.id) If you take the default … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)