<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Stephen N.</title>
    <description>The latest articles on DEV Community by Stephen N. (@interprimos80_ng).</description>
    <link>https://dev.to/interprimos80_ng</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F781545%2F58dfd7e5-1127-4c9f-8797-c7bd310c15f1.png</url>
      <title>DEV Community: Stephen N.</title>
      <link>https://dev.to/interprimos80_ng</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/interprimos80_ng"/>
    <language>en</language>
    <item>
      <title>Django Tutorial – Creating Dropdown Menus On Django Forms</title>
      <dc:creator>Stephen N.</dc:creator>
      <pubDate>Fri, 09 Sep 2022 18:08:43 +0000</pubDate>
      <link>https://dev.to/interprimos80_ng/django-tutorial-creating-dropdown-menus-on-django-forms-52nc</link>
      <guid>https://dev.to/interprimos80_ng/django-tutorial-creating-dropdown-menus-on-django-forms-52nc</guid>
      <description>&lt;p&gt;I have been teaching myself Python programming for the last couple of years and have recently started learning the Django framework for web development. I will be writing regular posts about some of the challenges I faced and their resolutions. This way, I can review these posts in the future if I need to and possibly benefit others who may experience these same challenges.&lt;/p&gt;

&lt;p&gt;This is my first time posting here, so I apologize in advance if there’s anything I miss. For this post, the challenge I faced (and resolved) was creating dropdown menus on Django forms. Below are the steps I followed in doing this; please keep in mind that I created my form using ModelForm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Inside your Django model class, define the different values of your dropdown menu and assign a constant to each value&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8DzYrtnX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7q1i3yrsoft284itmzi3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8DzYrtnX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7q1i3yrsoft284itmzi3.png" alt="Dropdown values" width="477" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Still inside your model class, create a list of the values in your dropdown menu. Each item on the list will be a tuple containing two elements. The first element in the tuple is the constant that was created in Step 1 and the second element is the human-readable name. It is the second element (the human readable name) that will be displayed on the Django form for your user.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EV6RrFum--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k80aasyzhcn0to5jj9lj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EV6RrFum--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k80aasyzhcn0to5jj9lj.png" alt="Values list" width="408" height="316"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Add the dropdown field to your database model. Assign your list from step 2 as the value to the keyword argument “choices”. If you want to, you can also set a default value to display for your dropdown menu&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YIRICt1f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8o5iew6pyqud5kmiy9h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YIRICt1f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8o5iew6pyqud5kmiy9h.png" alt="Django model" width="510" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; In your model form (usually in forms.py), use the “Select” attribute for your dropdown widget &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7p-NEGwl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uq1n63tngrm670r2q51w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7p-NEGwl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uq1n63tngrm670r2q51w.png" alt="Widget" width="877" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Once you launch your  Django form, you should see your dropdown with your values&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_4TdYCwV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ja1fwzzhz3h8ruac6y75.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_4TdYCwV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ja1fwzzhz3h8ruac6y75.png" alt="Django form" width="880" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>django</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
