python - how to edit {{form|crispy}} with css and move the forms around? -
i've been using {{form|crispy}} have decided design , move stuff around, i'm trying edit forms. have
{% block content %} <form id="post_form" method="post" action="/add_post/" enctype="multipart/form-data"> {% csrf_token %} {{ form|crispy }} <!--{% tag in person.tags.all %}{{ tag.word }} {% endfor %}--> <input type="submit" name="submit" value="create post"> </form> {% endblock %} in html file.
i have
class postform(forms.modelform): # category = categorychoices() title = forms.charfield(max_length=128, help_text="plz enter") url = forms.urlfield(max_length=200, help_text="please enter url of page.", required=false) views = forms.integerfield(widget=forms.hiddeninput(), initial=0) class meta: model = post widgets = { 'category':select2widget, } exclude = ['pub_date', 'moderator', 'rank_score','slug', 'image'] and in models.py
class post(models.model): category = models.foreignkey(category) pub_date = models.datetimefield(auto_now_add = true) url = models.urlfield(max_length=250, blank=true, null=true) video = embedvideofield(verbose_name='my video',blank=true, null=true) title = models.charfield(max_length = 100) moderator = models.foreignkey(user) views = models.integerfield(default=0) slug = models.charfield(max_length=100, unique=true) content = richtextuploadingfield(config_name='default') rank_score = models.floatfield(default= 1) image = models.imagefield(upload_to='images',blank=true, null=true) so in short have:category, url, video link, title, content in form. i'm trying move these forms around. how achieve goal? thank in advance.
besides option of rendering whole form can render each element of form seperately. recommend read through part of django documentation. example can this:
{{ form.non_field_errors }} <div class="fieldwrapper"> {{ form.subject.errors }} <label for="{{ form.subject.id_for_label }}">email subject:</label> {{ form.subject }} </div> <div class="fieldwrapper"> {{ form.message.errors }} <label for="{{ form.message.id_for_label }}">your message:</label> {{ form.message }} </div> for styling form, add css classes fields in __init__function of form class . example postform this:
def __init__(self, *args, **kwargs): super(postform, self).__init__(*args, **kwargs) field_name in self.fields: field = self.fields[field_name] field.widget.attrs.update({'class': 'form-control'}) this assign bootstrap class form-control every field in form. of course can style individual fields using method:
self.fields['title'].widget.attrs.update({'placeholder': 'this title'}) there might better solutions styling part. bigger forms little clunky. smaller forms handling right works fine.
Comments
Post a Comment