Home kellton

Main navigation

  • Services
    • Digital Business Services
      • Digital Experience
        • Product Strategy & Consulting
        • Product Design
        • Product Management
      • Product Engineering
        • Digital Application Development
        • Mobile Engineering
        • IoT & Wearables Solutions
        • Quality Engineering
      • Data Engineering & AI
        • Data Engineering
        • Data Science & ML
        • Generative AI & ChatGPT
        • Visualisation & Analytics
        • Integration & API
        • RPA
      • Cloud Engineering
        • Cloud Consulting
        • Cloud Migration
        • Cloud Managed Services
        • DevSecOps
      • NextGen Services
        • Blockchain
        • Web3
        • Metaverse
    • SAP
      • SAP Services
        • S/4HANA Implementations
        • SAP AMS Support
        • SAP Automation
        • SAP Security & GRC
        • SAP Value Added Solutions
        • Other SAP Implementations
  • Platforms & Products
    • Kellton4Health
    • Kellton4NFT
    • Kellton4Commerce
    • KLGAME
    • tHRive
    • Optima
  • Industries
    • Fintech, Banking, Financial Services & Insurance
    • Retail, E-Commerce & Distribution
    • Pharma, Healthcare & Life Sciences
    • Non-Profit, Government & Education
    • Travel, Logistics & Hospitality
    • HiTech, SaaS, ISV & Communications
    • Manufacturing, Automotive & Chemicals
    • Oil,Gas & Mining
    • Energy & Utilities
  • Insights
    • Blogs
    • Brochures
    • Success Stories
    • News / Announcements
    • Webinars
    • White papers
  • Careers
    • Life At Kellton
    • Jobs
  • About
    • About Us
    • Our Partners
    • Our Leadership
    • Testimonials
    • Investors
    • Privacy-Policy
    • Contact Us
Search

Breadcrumb

  1. Home
  2. Blogs
  3. Progressive enhancement with Django forms

Progressive enhancement with Django forms

Product Engineering
April 16th , 2018
Posted By:
Kellton
linkedin
Progressive enhancement with Django forms

Related Post

Apple Vision Pro
Apple Vision Pro: A leap into the future of Spatial Computing
07 Jun, 2023
Thumbnail - Why should you run SAP on AWS?
Why should you run SAP on AWS?
02 Jun, 2023
Mobile App Development Cost
How Much Does Mobile App Development Cost in 2023?
31 May, 2023

This blog was initially published on www.tivix.com (a subsidiary of Kellton). As a result of a merger of Kellton and Tivix all the content hosted at tivix.com has been merged with www.kellton.com. Happy Reading!

Batteries included Django, for better or worse, takes the approach of some web-frameworks such as Rails, and bundles as many utilities as necessary together with the core url-resolution framework, such as ORM, templating, administration, forms, and so on. This is in contrast to the philosophy of lighter-weight frameworks like Flask and platforms like node.js, which tend to take a more “mix-and-match” approach, encouraging the developer to pick out options for ORM, templating, forms, etc separately. A great deal can be said in favor of both approaches. While I prefer Django’s approach in general, one downside is it occasionally results in programmers fighting against the provided components instead of using them carefully and creatively.

 

Django Forms is sometimes a source of trouble. The default forms are ghastly, useless for modern UX concerns. Well-styled, flexible forms, with real-time validation and interactive, complicated widgets, are essential parts of a modern web app’s spec.

 

You might be tempted to completely disregard the forms feature, and just hardcode widgets in the templates, and pull in data from POST and validate data directly. While this is certainly a valid last resort or quick fix, it’s best to take advantage of the Django philosophy and do things in a well-structured and idiomatic way.

 

ms overview

 

Keep it simple

 

Graceful degradation to plain HTML forms for JavaScript-impaired browsers may seem like a bit of a blast from the past: there aren’t too many web users left without JavaScript in today’s web. However, this philosophy of implementation is particularly suited toward building easily readable and portable web applications in Django.

 

So, how do we do fancy custom widgets in Django forms, while still being able to do {{ form.as_p }}, or the equivalent? Depending on the level of customization needed, we can use the following process:
 

  • So, how do we do fancy custom widgets in Django forms, while still being able to do {{ form.as_p }}, or the equivalent? Depending on the level of customization needed, we can use the following process:

  • Not enough? Do you have some pre-baked JavaScript, such as from bootstrap, to toss in there? Using widget attributes, we can invoke JS enhancements on a plain Django field.

  • Need some custom JS enhancements? One thing to do, then, is attach a data-do-something sort of attribute to the widget, and then somewhere in your included JavaScript add the enhancement (e.g. $('[data-do-something]').each(function () { ... })), or perhaps just make the widget a hidden input, and put your JS and HTML in a template snippet.

  • At this point, you’ll probably need some custom validation or data processing. That’s easy enough too: go ahead and implement some clean_* fields on your form, or even create a custom FormField which correctly cleans and parses your data type.

  • Finally, if you truly hit a wall with the generated HTML, which you likely will with at least one form, it’s time to write a custom widget! Writing custom widgets can include arbitrary Python-generated HTML, completely obsoleting hardcoded HTML forms. If you don’t like the idea of including embedded HTML in Python, then include the render_to_string shortcut.

 

Of course, if you go through this process, and still feel you are struggling against Django instead of taking advantage of the power and flexibility of Django forms, then you might really be hitting a limitation with the framework, and throwing in some custom HTML and validation code won’t hurt in a pinch. However, I’ve found this a very rare circumstance. Using the above options, you can predictably produce re-usable components that are easily readable by other Django engineers.

Posted By:
Kellton
linkedin

Want to know more?

Mobile App Development Cost
Blog
How Much Does Mobile App Development Cost in 2023?
31 May, 2023
Drupal Development
Blog
5 Reasons why Drupal remains a top choice as a CMS
29 May, 2023
Thumbnail - Best Android automated app testing frameworks in 2023
Blog
Best Android automated app testing frameworks in 2023
08 May, 2023

Leading you through Digital Transformation journey

North America: +1.844.469.8900

Asia: +91.124.469.8900

Europe: +44.203.807.6911

Email: ask@kellton.com

Footer menu right

  • Digital Experience
  • Data Engineering & AI
  • Nextgen Services
  • About
  • Contact

Footer Menu Left

  • Product Engineering
  • Cloud Engineering
  • SAP Services
  • Careers
  • Success Stories
clutch Badge
GoodFirms Badge

© 2023 Kellton