0

Hello wonderful people.

I am currently learning Django and trying to display the database which is created via form on inventiory_view.html

I am unable to do so via shell scripting.

I am able to display the objects via shell terminal but whenever I use the same command which is Form1.objects.all I am unable to display it on my desired html page.

I would really appreciate your help in this matter.

Thanks in advance

This is the form1.html where I am getting the data from the user.

{% extends 'base.html' %}

<!doctype html>

<html lang="en">
  <head>

    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

{% block title %} Title of Form1 {% endblock %}
  </head>
  {% block body %}
  <div class="container">
    <h1>Form1</h1>
    <form method="POST" action=" /form1/">
        {% csrf_token %}
        <div class="form-group">
          <label for="exampleFormControlInput1">Item name</label>
          <input type="text" class="form-control" id="exampleFormControlInput1" name= "item" placeholder="laptop">
        </div>
        <!-- <div class="form-group">
            <label for="exampleFormControlInput1">Quantity </label>
            <input type="number" class="form-control" id="exampleFormControlInput1" name= "quantity" placeholder="1,2,3">
          </div>     -->
         <div class="form-group">
          <label for="exampleFormControlSelect1">Quantity</label>
          <select class="form-control" input type="number" id="exampleFormControlSelect1" name= "quantity">
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
            <option>5</option>
          </select>
        </div> 
        <div class="form-group">
            <label for="exampleFormControlInput1">Vendor name</label>
            <input type="text" class="form-control" id="exampleFormControlInput1" name= "vendor" placeholder="Nokia">
          </div>
          <div class="form-group">
            <label for="exampleFormControlInput1">Inward Number</label>
            <input type="number" class="form-control" id="exampleFormControlInput1" name= "inward" placeholder="2569">
          </div>
          <button type="submit" > Submit</button>
      </form>
      <a href="{% url 'export-csv' %}" class = "btn btn-secondary">Export CSV</a>
       {% endblock %}
     

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</html>

views.py

from django.shortcuts import render, HttpResponse
from form.models import Form1
from django.core.mail import send_mail
from django.conf import settings
from django.db import models
import csv

# Create your views here.
def index(request):
  return render(request, 'index.html')
   # return HttpResponse("homepage")

def form1(request):
    if request.method == "POST":
        item = request.POST.get('item')
        quantity = request.POST.get('quantity')
        vendor = request.POST.get('vendor')
        inward = request.POST.get('inward')
        form1 = Form1(item = item, quantity = quantity , vendor=vendor , inward= inward)
        form1.save()

        send_mail(
    'Test Email from IT team',
    'We have received ' +  str(quantity) + ' quantity of ' + item + ' from ' + vendor + ' \n Inward Register Number'  + str(inward)  ,
    # 'Hello, IT team. Whenever an inventory will be added you will get the email sent to you or anyone who is concerned' ,
    'techdevio20@example.com',
    ['sam.atharkar@gmail.com'],
    fail_silently=False,
)
    return render(request, 'form1.html')
    # return HttpResponse("Form 1 ")


def export_csv(request):

    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="inventory.csv"'

    writer= csv.writer(response)
    writer.writerow(['item', 'inward', 'quantity', 'vendor name'])

    inventory = Form1.objects.all()

models.py

from django.db import models

# Create your models here.
class Form1(models.Model):
    item = models.CharField(max_length=20)
    quantity = models.IntegerField()
    vendor = models.CharField(max_length=20)
    inward = models.IntegerField()

    def __str__(self):
        return self.item

urls.py

from django.contrib import admin
from django.urls import path
from form import views


urlpatterns = [
    path('',views.index, name = 'home'),
    path('form1/',views.form1, name = 'form1'),
    path('form2/',views.form2, name = 'form2'),
    path('login/',views.login, name = 'login'),
    path('signup/',views.signup, name = 'signup'),
    path('search/',views.search, name = 'search'),
    path('inventory_view/',views.inventory_view, name = 'inventory_view'),
    
]
SLDem
  • 2,065
  • 1
  • 8
  • 28
Sameer Atharkar
  • 382
  • 1
  • 3
  • 17
  • Have you look in the Django documentation, you have great tutorials to do exactly this type of thing. I suggest you to use the ModelForm builtin : https://docs.djangoproject.com/en/stable/topics/forms/modelforms/ – Benbb96 Jan 20 '21 at 09:55

1 Answers1

0

What you can do is create a seperate view to check all your objects like this:

def form1_entries(request):
    context = {}
    entries = Form1.objects.all()
    context['entries'] = entries
    return render(request, 'form1_entries.html', context)

Then in your form1_entries.html do this:

{% for entry in entries %}
    {{ entry.item }} 
    {{ entry.quantity }}
    {{ entry.vendor }}
    {{ entry.inward }}
{% endfor %}

Or just query the entries in the view where you have your form(form1) and pass them in the context of that view, then ofcourse you would have to add the html I included to your other template as well.

SLDem
  • 2,065
  • 1
  • 8
  • 28