Friday, April 24, 2015

ModelForm not creating field with required options

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>

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

No comments:

Post a Comment