from django import forms from phonenumber_field.formfields import PhoneNumberField from .models import Shop class ShopForm(forms.ModelForm): phone = PhoneNumberField( region='BY', required=False, help_text='Формат: +375XXXXXXXXX или 80XXXXXXXXX', widget=forms.TextInput(attrs={'placeholder': '+375XXXXXXXXX'}) ) class Meta: model = Shop fields = [ 'name', 'description', 'street', 'building_number', 'phone', 'email', 'is_active', 'is_pickup_point', ] widgets = { 'description': forms.Textarea(attrs={'rows': 3}), } def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # Ensure phone displays in E.164 format if self.instance and self.instance.phone: self.initial['phone'] = str(self.instance.phone) # Mark name field as required with label self.fields['name'].label = 'Название магазина *' self.fields['name'].required = True for field_name, field in self.fields.items(): if field_name == 'description': # Textarea already has rows=3 from widget, just add class field.widget.attrs.update({'class': 'form-control'}) elif field_name in ['is_active', 'is_pickup_point']: # Checkbox fields need form-check-input class field.widget.attrs.update({'class': 'form-check-input'}) elif field_name == 'phone': # Phone field gets form-control class field.widget.attrs.update({'class': 'form-control'}) else: # Regular input fields get form-control class field.widget.attrs.update({'class': 'form-control'}) # Add required attribute to HTML for name field if field_name == 'name': field.widget.attrs.update({'required': 'required'})