Home > Enterprise >  Django change textfield
Django change textfield

Time:05-04

Currently am using this textfield for my model

#model
class NoLoginPsuedoAppointment(models.Model):
    ...
    comments = models.TextField(blank=True)


#form
class NoLoginPsuedoAppointmentForm(forms.ModelForm):
    comments = forms.Textarea()

    class Meta:
        model = NoLoginPsuedoAppointment
        fields = [
            "comments",
        ]

Which comes out looking like this

enter image description here

Is there a way to change where the textarea begins so that comments is on top of the textarea or on its top left instead of being on its bottom left? Can this be changed through the django forms? or do I need to change it through css?

CodePudding user response:

sure you can change it. that's only the form field label you can put it anywhere you want. I guess you rendered the form as {{ form.as_p }} or as table or ... so you have to loop through the form fields

<form ... >
  {% csrf_token %} <!-- if thats POST -->
  {% for field in form %}
    <div >
      <div >{{ field.label }}</div>
      <div >
        {{ field }}
      </div>
    </div>
  {% endfor %}
<button type="submit" >Save Changes</button>
</form>

in above example I just created a row and putted the label and field in the same row, you can put it as you wish. and also you can access the field errors with {{ field.errors }}.

  • Related