ModelForm not creating field with required options

33 views
Skip to first unread message

victor menezes

unread,
Apr 24, 2015, 3:30:19 PM4/24/15
to django...@googlegroups.com
Hi all,

I'm using Django 1.8 and having trouble to because my form does not generate fields with required option even though they have the property "blank=False" in the model.
In the example bellow, shouldn't the field "description" have the "required" attribute in the html generated using "form.as_p" or am I missing anything?

models.py
class Place(models.Model):
    name = models.CharField(max_length=200, blank=False, default='')
    owners = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=False)
    members = models.ManyToManyField(Member, blank=True)
    description =  models.TextField(blank=False)
    location = models.CharField(max_length=400, blank=True)
    additional_info = models.TextField(blank=True)

    def __unicode__(self):
        return self.name


class PlaceForm(ModelForm):
    class Meta:
        model = Place
        fields = '__all__'

views.py
@login_required(login_url='/')
def addPlaceView(request):
    place_form = PlaceForm()
    context = {
        'form': place_form,
    }
    return render(request, 'newPlace.html', context)

newPlace.html
<form action="/newplace/" method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit" />
</form>

Stephen J. Butler

unread,
Apr 25, 2015, 5:53:23 AM4/25/15
to django...@googlegroups.com
You need to set Form.required_css_class

https://docs.djangoproject.com/en/1.8/ref/forms/api/#styling-required-or-erroneous-form-rows
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/84a2cdf8-f6de-438b-b7d3-775c4300f105%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages