Ddjango データをdbにインポート

# views.py

from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse
import tkinter as tk
import pandas as pd
from io import StringIO
from .forms import TextAreaForm
from .models import MyData

def index(request):
          return render(request, 'travel/index.html')

def test(request):
    if request.method == 'POST':
        form = TextAreaForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data['text']
            df = pd.read_csv(StringIO(data), sep='\t')
            empty_df = df.empty
            
            # データベースに取り込む
            for index, row in df.iterrows():
                imported_data = MyData()
                for i in range(len(row)):
                    setattr(imported_data, f'column{i+1}', row[i])
                imported_data.save()

    else:
        form = TextAreaForm()
        df = pd.DataFrame()
        empty_df = df.empty

    context = {'form': form, 'df': df, 'empty_df': empty_df}
    return render(request, 'travel/test.html', context)

# test.html

{% extends 'travel/base.html' %}
{% block content %}
{% load static %}

<style>
  /* テーブルのスタイル */
table {
          border-collapse: collapse;
          width: 100%;
        }
        
        th, td {
          text-align: left;
          padding: 8px;
        }
        
        th {
          background-color: #1E90FF;
          color: white;
        }
        
        tr:nth-child(even) {
          background-color: #f2f2f2
        }
        
        /* フォームのスタイル */
        form {
          width: 100%;
          max-width: 800px;
          margin: 0 auto;
          padding: 20px;
          background-color: #f2f2f2;
          border-radius: 5px;
          box-shadow: 0px 0px 5px #999;
        }
        
        label {
          display: block;
          margin-bottom: 5px;
          font-weight: bold;
        }
        
        input[type="text"], textarea {
          width: 100%;
          padding: 8px;
          border-radius: 5px;
          border: none;
          box-shadow: 0px 0px 5px #999;
          resize: none;
        }
        
        button[type="submit"] {
          background-color: #1E90FF;
          color: white;
          padding: 8px 20px;
          border-radius: 5px;
          border: none;
          font-size: 16px;
          cursor: pointer;
        }
        
        button[type="submit"]:hover {
          background-color: #009ACD;
        }
        
        /* エラーメッセージのスタイル */
        .errorlist {
          color: red;
          list-style: none;
          margin: 0;
          padding: 0;
          font-weight: bold;
        }
        
        .errorlist li {
          margin-bottom: 5px;
        }

</style>


<h1>Import Data</h1>

<form method="post">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Import</button>
</form>


{% if not empty_df %}
  <table border="1">
    <thead>
      <tr>
        {% for column in df.columns %}
          <th>{{ column }}</th>
        {% endfor %}
      </tr>
    </thead>
    <tbody>
      {% for index, row in df.iterrows %}
        <tr>
          {% for value in row %}
            <td>{{ value }}</td>
          {% endfor %}
        </tr>
      {% endfor %}
    </tbody>
  </table>
{% endif %}



{% endblock %}
``

{% extends 'travel/base.html' %}
{% block content %}
{% load static %}

{% extends 'travel/base.html' %}
{% block content %}
{% load static %}

<style>
  /* テーブルのスタイル */
table {
          border-collapse: collapse;
          width: 100%;
        }
        
        th, td {
          text-align: left;
          padding: 8px;
        }
        
        th {
          background-color: #1E90FF;
          color: white;
        }
        
        tr:nth-child(even) {
          background-color: #f2f2f2
        }
        
        /* フォームのスタイル */
        form {
          width: 100%;
          max-width: 800px;
          margin: 0 auto;
          padding: 20px;
          background-color: #f2f2f2;
          border-radius: 5px;
          box-shadow: 0px 0px 5px #999;
        }
        
        label {
          display: block;
          margin-bottom: 5px;
          font-weight: bold;
        }
        
        input[type="text"], textarea {
          width: 100%;
          padding: 8px;
          border-radius: 5px;
          border: none;
          box-shadow: 0px 0px 5px #999;
          resize: none;
        }
        
        button[type="submit"] {
          background-color: #1E90FF;
          color: white;
          padding: 8px 20px;
          border-radius: 5px;
          border: none;
          font-size: 16px;
          cursor: pointer;
        }
        
        button[type="submit"]:hover {
          background-color: #009ACD;
        }
        
        /* エラーメッセージのスタイル */
        .errorlist {
          color: red;
          list-style: none;
          margin: 0;
          padding: 0;
          font-weight: bold;
        }
        
        .errorlist li {
          margin-bottom: 5px;
        }

</style>


<h1>Import Data</h1>

<form method="post">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Import</button>
</form>


{% if not empty_df %}
  <table border="1">
    <thead>
      <tr>
        {% for column in df.columns %}
          <th>{{ column }}</th>
        {% endfor %}
      </tr>
    </thead>
    <tbody>
      {% for index, row in df.iterrows %}
        <tr>
          {% for value in row %}
            <td>{{ value }}</td>
          {% endfor %}
        </tr>
      {% endfor %}
    </tbody>
  </table>
{% endif %}



{% endblock %}
``


# forms.py
from django import forms

class TextAreaForm(forms.Form):
    text = forms.CharField(widget=forms.Textarea(attrs={'rows': 10}))