{"id":75675,"date":"2020-01-20T11:29:52","date_gmt":"2020-01-20T05:59:52","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=75675"},"modified":"2024-04-12T14:17:14","modified_gmt":"2024-04-12T08:47:14","slug":"a-custom-look-and-feel","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/","title":{"rendered":"A Custom Look and Feel"},"content":{"rendered":"<p>The quickest way to customize the form\u2019s presentation is with CSS. The list of errors in particular could do with some visual enhancement, and the &lt;ul&gt; has a class attribute of errorlist for that exact purpose. The following CSS really makes our errors stand out:<\/p>\n<p>&lt;style type=&#8221;text\/css&#8221;&gt;<br>\nul.errorlist {<br>\nmargin: 0;<br>\npadding: 0;<br>\n}<br>\n.errorlist li {<br>\nbackground-color: red;<br>\ncolor: white;<br>\ndisplay: block;<br>\nfont-size: 10px;<br>\nmargin: 0 0 3px;<br>\npadding: 4px 5px;<br>\n}<br>\n&lt;\/style&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>While it\u2019s convenient to have our form\u2019s HTML generated for us, in many cases the default rendering won\u2019t be right for our application. {{ form.as_table }} and friends are useful shortcuts while we develop our application, but everything about the way a form is displayed can be overridden, mostly within the template itself.<\/p>\n<p>Each field widget (&lt;input type=&#8221;text&#8221;&gt;, &lt;select&gt;, &lt;textarea&gt;, or similar) can be rendered individually by accessing {{ form.fieldname }}. Any errors associated with a field are available as {{ form.fieldname.errors }}. We can use these form variables to construct a custom template for our contact form:<\/p>\n<p>&lt;form action=&#8221;.&#8221; method=&#8221;POST&#8221;&gt;<br>\n&lt;div class=&#8221;fieldWrapper&#8221;&gt;<br>\n{{ form.topic.errors }}<br>\n&lt;label for=&#8221;id_topic&#8221;&gt;Kind of feedback:&lt;\/label&gt;<br>\n{{ form.topic }}<br>\n&lt;\/div&gt;<br>\n&lt;div class=&#8221;fieldWrapper&#8221;&gt;<br>\n{{ form.message.errors }}<br>\n&lt;label for=&#8221;id_message&#8221;&gt;Your message:&lt;\/label&gt;<br>\n{{ form.message }}<br>\n&lt;\/div&gt;<br>\n&lt;div class=&#8221;fieldWrapper&#8221;&gt;<br>\n{{ form.sender.errors }}<br>\n&lt;label for=&#8221;id_sender&#8221;&gt;Your email (optional):&lt;\/label&gt;<br>\n{{ form.sender }}<br>\n&lt;\/div&gt;<br>\n&lt;p&gt;&lt;input type=&#8221;submit&#8221; value=&#8221;Submit&#8221;&gt;&lt;\/p&gt;<br>\n&lt;\/form&gt;<\/p>\n<p>{{ form.message.errors }} will display as a &lt;ul class=&#8221;errorlist&#8221;&gt; if errors are present and a blank string if the field is valid (or the form is unbound). We can also treat form.message.errors as a Boolean or even iterate over it as a list, for example:<\/p>\n<p>&lt;div class=&#8221;fieldWrapper{% if form.message.errors %} errors{% endif %}&#8221;&gt;<br>\n{% if form.message.errors %}<br>\n&lt;ol&gt;<br>\n{% for error in form.message.errors %}<br>\n&lt;li&gt;&lt;strong&gt;{{ error|escape }}&lt;\/strong&gt;&lt;\/li&gt;<br>\n{% endfor %}<br>\n&lt;\/ol&gt;<br>\n{% endif %}<br>\n{{ form.message }}<br>\n&lt;\/div&gt;<\/p>\n<p>In the case of validation errors, this will add an \u201cerrors\u201d class to the containing &lt;div&gt; and display the list of errors in an ordered list.<\/p>\n<h3>Specifying labels<\/h3>\n<p>By default, the labels on Django\u2019s auto-generated form HTML are created by replacing underscores with spaces and capitalizing the first letter \u2013 so the label for the email field is \u201cEmail\u201c. (Sound familiar? It\u2019s the same simple algorithm that Django\u2019s models use to calculate default verbose_name values for fields.) But, as with Django\u2019s models, we can customize the label for a given field. Just use label, like so:<\/p>\n<p>class ContactForm(forms.Form):<br>\nsubject = forms.CharField(max_length=100)<br>\nemail = forms.EmailField(required=False, label=&#8217;Your e-mail address&#8217;)<br>\nmessage = forms.CharField(widget=forms.Textarea)<\/p>\n<h3>Customizing Form Design<\/h3>\n<p>Our contact_form.html template uses {{ form.as_table }} to display the form, but we can display the form in other ways to get more granular control over display. The quickest way to customize forms\u2019 presentation is with CSS. Error lists, in particular, could do with some visual enhancement, and the auto-generated error lists use &lt;ul class=&#8221;errorlist&#8221;&gt; precisely so that you can target them with CSS. Add the following CSS in the &lt;head&gt;&lt;\/head&gt; section of contact_form.html to really make our errors stand out (Figure 6-9):<\/p>\n<p>&lt;style type=&#8221;text\/css&#8221;&gt;<br>\nul.errorlist {<br>\nmargin: 0;<br>\npadding: 0;<br>\n}<br>\n.errorlist li {<br>\nbackground-color: red;<br>\ncolor: white;<br>\ndisplay: block;<br>\nfont-size: 1.2em;<br>\nmargin: 0 0 3px;<br>\npadding: 4px 5px;<br>\n}<br>\n&lt;\/style&gt;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-128619\" src=\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/35-380x214.jpg\" alt=\"\" width=\"380\" height=\"214\"><\/p>\n<p>While it\u2019s convenient to have our form\u2019s HTML generated for us, in many cases you\u2019ll want to override the default rendering. {{ form.as_table }} and friends are useful shortcuts while you develop your application, but everything about the way a form is displayed can be overridden, mostly within the template itself, and you\u2019ll probably find yourself doing this.<\/p>\n<p>Each field\u2019s widget (&lt;input type=&#8221;text&#8221;&gt;, &lt;select&gt;, &lt;textarea&gt;, etc.) can be rendered individually by accessing {{ form.fieldname }} in the template, and any errors associated with a field are available as {{ form.fieldname.errors }}. With this in mind, we can construct a custom template for our contact form with the following template code:<\/p>\n<p>&lt;html&gt;<br>\n&lt;head&gt;<br>\n&lt;title&gt;Contact us&lt;\/title&gt;<br>\n&lt;\/head&gt;<br>\n&lt;body&gt;<br>\n&lt;h1&gt;Contact us&lt;\/h1&gt;<\/p>\n<p>{% if form.errors %}<br>\n&lt;p style=&#8221;color: red;&#8221;&gt;<br>\nPlease correct the error{{ form.errors|pluralize }} below.<br>\n&lt;\/p&gt;<br>\n{% endif %}<\/p>\n<p>&lt;form action=&#8221;&#8221; method=&#8221;post&#8221;&gt;<br>\n&lt;div class=&#8221;field&#8221;&gt;<br>\n{{ form.subject.errors }}<br>\n&lt;label for=&#8221;id_subject&#8221;&gt;Subject:&lt;\/label&gt;<br>\n{{ form.subject }}<br>\n&lt;\/div&gt;<br>\n&lt;div class=&#8221;field&#8221;&gt;<br>\n{{ form.email.errors }}<br>\n&lt;label for=&#8221;id_email&#8221;&gt;Your e-mail address:&lt;\/label&gt;<br>\n{{ form.email }}<br>\n&lt;\/div&gt;<br>\n&lt;div class=&#8221;field&#8221;&gt;<br>\n{{ form.message.errors }}<br>\n&lt;label for=&#8221;id_message&#8221;&gt;Message:&lt;\/label&gt;<br>\n{{ form.message }}<br>\n&lt;\/div&gt;<br>\n{% csrf_token %}<br>\n&lt;input type=&#8221;submit&#8221; value=&#8221;Submit&#8221;&gt;<br>\n&lt;\/form&gt;<br>\n&lt;\/body&gt;<br>\n&lt;\/html&gt;<\/p>\n<p>In the above template code, {{ form.message.errors }} displays a &lt;ul class=&#8221;errorlist&#8221;&gt; if errors are present and a blank string if the field is valid (or the form is unbound). This is the same for the other fields on our contact form.<\/p>\n<p>We can also treat form.message.errors as a Boolean or even iterate over it as a list. This is useful when there can be multiple errors associated with a field. For example:<\/p>\n<p>&lt;div class=&#8221;field{% if form.message.errors %} errors{% endif %}&#8221;&gt;<br>\n{% if form.message.errors %}<br>\n&lt;ul&gt;<br>\n{% for error in form.message.errors %}<br>\n&lt;li&gt;&lt;strong&gt;{{ error }}&lt;\/strong&gt;&lt;\/li&gt;<br>\n{% endfor %}<br>\n&lt;\/ul&gt;<br>\n{% endif %}<br>\n&lt;label for=&#8221;id_message&#8221;&gt;Message:&lt;\/label&gt;<br>\n{{ form.message }}<br>\n&lt;\/div&gt;<\/p>\n<p>In the case of validation errors, this will add an \u201cerrors\u201d class to the containing &lt;div&gt; and display the list of errors associated with the message field in an unordered list.<\/p>\n\n\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/certified-django-developer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Back to Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The quickest way to customize the form\u2019s presentation is with CSS. The list of errors in particular could do with some visual enhancement, and the &lt;ul&gt; has a class attribute of errorlist for that exact purpose. The following CSS really makes our errors stand out: &lt;style type=&#8221;text\/css&#8221;&gt; ul.errorlist { margin: 0; padding: 0; } .errorlist&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8655],"tags":[8773],"class_list":["post-75675","page","type-page","status-publish","hentry","category-django-web-development","tag-a-custom-look-and-feel"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>A Custom Look and Feel - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A Custom Look and Feel - Tutorial\" \/>\n<meta property=\"og:description\" content=\"The quickest way to customize the form\u2019s presentation is with CSS. The list of errors in particular could do with some visual enhancement, and the &lt;ul&gt; has a class attribute of errorlist for that exact purpose. The following CSS really makes our errors stand out: &lt;style type=&#8221;text\/css&#8221;&gt; ul.errorlist { margin: 0; padding: 0; } .errorlist...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:47:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/35-380x214.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/\",\"name\":\"A Custom Look and Feel - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/35-380x214.jpg\",\"datePublished\":\"2020-01-20T05:59:52+00:00\",\"dateModified\":\"2024-04-12T08:47:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/#primaryimage\",\"url\":\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/35-380x214.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/35-380x214.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"A Custom Look and Feel\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A Custom Look and Feel - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/","og_locale":"en_US","og_type":"article","og_title":"A Custom Look and Feel - Tutorial","og_description":"The quickest way to customize the form\u2019s presentation is with CSS. The list of errors in particular could do with some visual enhancement, and the &lt;ul&gt; has a class attribute of errorlist for that exact purpose. The following CSS really makes our errors stand out: &lt;style type=&#8221;text\/css&#8221;&gt; ul.errorlist { margin: 0; padding: 0; } .errorlist...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:47:14+00:00","og_image":[{"url":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/35-380x214.jpg","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/","name":"A Custom Look and Feel - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/#primaryimage"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/#primaryimage"},"thumbnailUrl":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/35-380x214.jpg","datePublished":"2020-01-20T05:59:52+00:00","dateModified":"2024-04-12T08:47:14+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/#primaryimage","url":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/35-380x214.jpg","contentUrl":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/35-380x214.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/a-custom-look-and-feel\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"A Custom Look and Feel"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75675","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=75675"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75675\/revisions"}],"predecessor-version":[{"id":83353,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75675\/revisions\/83353"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=75675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=75675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=75675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}