<?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: Luke</title>
    <description>The latest articles on DEV Community by Luke (@robotichead).</description>
    <link>https://dev.to/robotichead</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F477361%2Fc6c7007f-974e-42f8-9745-f54ebfa9c535.jpeg</url>
      <title>DEV Community: Luke</title>
      <link>https://dev.to/robotichead</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/robotichead"/>
    <language>en</language>
    <item>
      <title>Luxon to Django Forms</title>
      <dc:creator>Luke</dc:creator>
      <pubDate>Thu, 08 Oct 2020 23:35:31 +0000</pubDate>
      <link>https://dev.to/robotichead/luxon-to-django-forms-i33</link>
      <guid>https://dev.to/robotichead/luxon-to-django-forms-i33</guid>
      <description>&lt;p&gt;The following is a micro tutorial, on how I sent dates from the front end using Luxon to my backend Django.&lt;/p&gt;

&lt;h1&gt;
  
  
  Dot Point Context
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;User will record start and end date for their project in NearBeach &lt;/li&gt;
&lt;li&gt;The datetime picker used is &lt;a href="https://github.com/mariomka/vue-datetime" rel="noopener noreferrer"&gt;vue-datetime&lt;/a&gt; for the datetime picker.&lt;/li&gt;
&lt;li&gt;Vue-datetime uses the new &lt;a href="https://moment.github.io/luxon/" rel="noopener noreferrer"&gt;Luxon&lt;/a&gt; library&lt;/li&gt;
&lt;li&gt;The project I am talking about is this micro tutorial is &lt;a href="https://github.com/robotichead/NearBeach" rel="noopener noreferrer"&gt;NearBeach&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4joazd9zn4d4ck4lyi6m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4joazd9zn4d4ck4lyi6m.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Method
&lt;/h1&gt;

&lt;p&gt;The vue-datetime widget wants the datetime input to be a simple string. So - lets convert&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.localEndDateModel = this.endDateModel.toISO();
this.localStartDateModel = this.startDateModel.toISO();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the user hits the submit button, we use Axios to send the data through the POST method. First we have to compile the data we want to send&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Create data_to_send
const data_to_send = new FormData();
data_to_send.set('project_name',this.projectNameModel);
data_to_send.set('project_description',this.projectDescriptionModel);
data_to_send.set('organisation',this.stakeholderModel['value']);
data_to_send.set('project_start_date',this.projectStartDateModel);
data_to_send.set('project_end_date',this.projectEndDateModel);

// Insert a new row for each group list item
this.groupModel.forEach((row,index) =&amp;gt; {
    data_to_send.append(`group_list`,row['value']);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see in the code - it does not matter that we are sending the string value. We will be modifying the Django Form to accept this string value.&lt;/p&gt;

&lt;p&gt;In the Django form, we want to edit the input format for the datetime fields to accept the &lt;a href="https://en.wikipedia.org/wiki/ISO_8601" rel="noopener noreferrer"&gt;ISO_8061&lt;/a&gt;. This requires placing the attribute &lt;code&gt;input_formats=['c'],&lt;/code&gt; inside the DateTimeField.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class NewProjectForm(forms.ModelForm):
    project_start_date = forms.DateTimeField(
        input_formats=['c'],
    )
    project_end_date = forms.DateTimeField(
        input_formats=['c'],
    )
    group_list = forms.ModelMultipleChoiceField(
        required=True,
        queryset=group.objects.filter(
            is_deleted=False,
        )
    )

    # Basic Meta Data
    class Meta:
        model = project
        fields = [
            'project_name',
            'project_description',
            'project_start_date',
            'project_end_date',
            'organisation',
        ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The form will now accept your Luxon iso string.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick notes
&lt;/h2&gt;

&lt;p&gt;The ISO 8061 standard was implemented in Django 3.1 as discussed in the &lt;a href="https://docs.djangoproject.com/en/3.1/releases/3.1/" rel="noopener noreferrer"&gt;release notes&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>django</category>
      <category>vue</category>
      <category>luxon</category>
    </item>
  </channel>
</rss>
