Failed to save the data in DB in Django












0















I am trying to upload a CSV file to Django, below depicted code is well and no errors popping up but post submit the CSV, data is not being stored in Django db. when I look at the admin page no details present.



models.py



from django.db import models

class Shiftrotainfo(models.Model):
first_name=models.CharField(max_length=50)
last_name=models.CharField(max_length=50)
whichShift = models.CharField(max_length=50)

def __str__(self):
return f'{self.first_name} {self.last_name}'


views.py



from django.shortcuts import render
from django.http import HttpResponse
import csv,io
from django.contrib import messages
from .models import Shiftrotainfo

def home(request):
return HttpResponse('<h1>Shift Rota</h1>')

def upload_rota(request):
template = "shiftrota/rota_upload.html"
prompt = {
'order':'order of the CSV name followed by week shift details'
}
if request.method == "GET":
return render(request,template, prompt)

csv_file = request.FILES['file']
if not csv_file.name.endswith('.csv'):
messages.error(request,'This is not a CSV file')

data_set = csv_file.read().decode('UTF-8')
io_string = io.StringIO(data_set)
next(io_string)

for column in csv.reader(io_string, delimiter=',', quotechar="|"):
_,created = Shiftrotainfo.objects.update_or_create(
first_name=column[0],
last_name=column[1],
whichShift=column[2]

)
context ={}
return render(request, template, context)


here rota_upload.html



{% if messages %}
{% for message in messages %}
<div {% if message.tags %} class="{{ message.tags }}" {% endif %}>
<strong>{{ message|safe }}</strong>
</div>
{% endfor %}
{% else %}
{{order}}
<form method="post" enctype="multipart/form-data">{% csrf_token %}
<label>Upload File</label>
<input type="file" name="file">
<p>Only Accepts CSV Files</p>
<button type="save">Upload</button>
</form>
{% endif %}


kindly suggest , did I miss any thing to add ?
Here is my fullcode : https://github.com/srinivasgadi77/schedule



Can you body assist here










share|improve this question





























    0















    I am trying to upload a CSV file to Django, below depicted code is well and no errors popping up but post submit the CSV, data is not being stored in Django db. when I look at the admin page no details present.



    models.py



    from django.db import models

    class Shiftrotainfo(models.Model):
    first_name=models.CharField(max_length=50)
    last_name=models.CharField(max_length=50)
    whichShift = models.CharField(max_length=50)

    def __str__(self):
    return f'{self.first_name} {self.last_name}'


    views.py



    from django.shortcuts import render
    from django.http import HttpResponse
    import csv,io
    from django.contrib import messages
    from .models import Shiftrotainfo

    def home(request):
    return HttpResponse('<h1>Shift Rota</h1>')

    def upload_rota(request):
    template = "shiftrota/rota_upload.html"
    prompt = {
    'order':'order of the CSV name followed by week shift details'
    }
    if request.method == "GET":
    return render(request,template, prompt)

    csv_file = request.FILES['file']
    if not csv_file.name.endswith('.csv'):
    messages.error(request,'This is not a CSV file')

    data_set = csv_file.read().decode('UTF-8')
    io_string = io.StringIO(data_set)
    next(io_string)

    for column in csv.reader(io_string, delimiter=',', quotechar="|"):
    _,created = Shiftrotainfo.objects.update_or_create(
    first_name=column[0],
    last_name=column[1],
    whichShift=column[2]

    )
    context ={}
    return render(request, template, context)


    here rota_upload.html



    {% if messages %}
    {% for message in messages %}
    <div {% if message.tags %} class="{{ message.tags }}" {% endif %}>
    <strong>{{ message|safe }}</strong>
    </div>
    {% endfor %}
    {% else %}
    {{order}}
    <form method="post" enctype="multipart/form-data">{% csrf_token %}
    <label>Upload File</label>
    <input type="file" name="file">
    <p>Only Accepts CSV Files</p>
    <button type="save">Upload</button>
    </form>
    {% endif %}


    kindly suggest , did I miss any thing to add ?
    Here is my fullcode : https://github.com/srinivasgadi77/schedule



    Can you body assist here










    share|improve this question



























      0












      0








      0








      I am trying to upload a CSV file to Django, below depicted code is well and no errors popping up but post submit the CSV, data is not being stored in Django db. when I look at the admin page no details present.



      models.py



      from django.db import models

      class Shiftrotainfo(models.Model):
      first_name=models.CharField(max_length=50)
      last_name=models.CharField(max_length=50)
      whichShift = models.CharField(max_length=50)

      def __str__(self):
      return f'{self.first_name} {self.last_name}'


      views.py



      from django.shortcuts import render
      from django.http import HttpResponse
      import csv,io
      from django.contrib import messages
      from .models import Shiftrotainfo

      def home(request):
      return HttpResponse('<h1>Shift Rota</h1>')

      def upload_rota(request):
      template = "shiftrota/rota_upload.html"
      prompt = {
      'order':'order of the CSV name followed by week shift details'
      }
      if request.method == "GET":
      return render(request,template, prompt)

      csv_file = request.FILES['file']
      if not csv_file.name.endswith('.csv'):
      messages.error(request,'This is not a CSV file')

      data_set = csv_file.read().decode('UTF-8')
      io_string = io.StringIO(data_set)
      next(io_string)

      for column in csv.reader(io_string, delimiter=',', quotechar="|"):
      _,created = Shiftrotainfo.objects.update_or_create(
      first_name=column[0],
      last_name=column[1],
      whichShift=column[2]

      )
      context ={}
      return render(request, template, context)


      here rota_upload.html



      {% if messages %}
      {% for message in messages %}
      <div {% if message.tags %} class="{{ message.tags }}" {% endif %}>
      <strong>{{ message|safe }}</strong>
      </div>
      {% endfor %}
      {% else %}
      {{order}}
      <form method="post" enctype="multipart/form-data">{% csrf_token %}
      <label>Upload File</label>
      <input type="file" name="file">
      <p>Only Accepts CSV Files</p>
      <button type="save">Upload</button>
      </form>
      {% endif %}


      kindly suggest , did I miss any thing to add ?
      Here is my fullcode : https://github.com/srinivasgadi77/schedule



      Can you body assist here










      share|improve this question
















      I am trying to upload a CSV file to Django, below depicted code is well and no errors popping up but post submit the CSV, data is not being stored in Django db. when I look at the admin page no details present.



      models.py



      from django.db import models

      class Shiftrotainfo(models.Model):
      first_name=models.CharField(max_length=50)
      last_name=models.CharField(max_length=50)
      whichShift = models.CharField(max_length=50)

      def __str__(self):
      return f'{self.first_name} {self.last_name}'


      views.py



      from django.shortcuts import render
      from django.http import HttpResponse
      import csv,io
      from django.contrib import messages
      from .models import Shiftrotainfo

      def home(request):
      return HttpResponse('<h1>Shift Rota</h1>')

      def upload_rota(request):
      template = "shiftrota/rota_upload.html"
      prompt = {
      'order':'order of the CSV name followed by week shift details'
      }
      if request.method == "GET":
      return render(request,template, prompt)

      csv_file = request.FILES['file']
      if not csv_file.name.endswith('.csv'):
      messages.error(request,'This is not a CSV file')

      data_set = csv_file.read().decode('UTF-8')
      io_string = io.StringIO(data_set)
      next(io_string)

      for column in csv.reader(io_string, delimiter=',', quotechar="|"):
      _,created = Shiftrotainfo.objects.update_or_create(
      first_name=column[0],
      last_name=column[1],
      whichShift=column[2]

      )
      context ={}
      return render(request, template, context)


      here rota_upload.html



      {% if messages %}
      {% for message in messages %}
      <div {% if message.tags %} class="{{ message.tags }}" {% endif %}>
      <strong>{{ message|safe }}</strong>
      </div>
      {% endfor %}
      {% else %}
      {{order}}
      <form method="post" enctype="multipart/form-data">{% csrf_token %}
      <label>Upload File</label>
      <input type="file" name="file">
      <p>Only Accepts CSV Files</p>
      <button type="save">Upload</button>
      </form>
      {% endif %}


      kindly suggest , did I miss any thing to add ?
      Here is my fullcode : https://github.com/srinivasgadi77/schedule



      Can you body assist here







      django django-models django-views






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 14:07







      Srinivas Gadi

















      asked Nov 18 '18 at 2:30









      Srinivas GadiSrinivas Gadi

      356




      356
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357375%2ffailed-to-save-the-data-in-db-in-django%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357375%2ffailed-to-save-the-data-in-db-in-django%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga