0
votes

I am building a private website for inventory management using python django, and i was creating a form to add items in my Item Model, till now I have redone my form code 2-3 times but am always getting stuck with same error "Expected table or queryset, not str" and have run out of ideas. here is my code,

views.py:

from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse
from .models import Item, Tender
from django.urls import reverse
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import login, logout, authenticate
from django.contrib import messages
from .forms import NewUserForm, AddItemForm
from .tables import ItemTable


def item_list(request):
    table = ItemTable(Item.objects.all())

    return render(request, "main/item_list.html", {
        "table": table
    })

def add_item(request):
if request.method == 'POST':
    form = AddItemForm(request.POST)
    if form.is_valid():
        Item = form.save(commit = False)
        Item.item_updated = timezone.now()
        Item.save()
        return redirect("main:item_list")

else:
    form = AddItemForm()


return render(request, "main/item_list.html", {'form': form})

models.py:

from django.db import models
from datetime import datetime
from django.contrib.auth.models import User

class Item(models.Model):
    item_no = models.CharField(max_length=200, primary_key = True )
    item_name = models.CharField(max_length = 200)
    item_quantity = models.CharField(max_length = 200, default = 0)
    item_description = models.TextField(blank = True)
    item_updated = models.DateTimeField("Item updated", default=datetime.now())
    item_price = models.CharField(max_length = 100, default = 0)

    def __str__(self):
        return self.item_name

forms.py:

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from .models import Item
from datetime import datetime

class AddItemForm(forms.ModelForm):

    class Meta:
        model = Item
        fields = (
            'item_no', 'item_name', 'item_quantity', 'item_description', 'item_price'
        )

add_item.html:

{% extends "main/header.html" %}

    {% block content %}
        <h1>Add Item Form</h1>

        <form method="POST" class="post-form">
            {% csrf_token %}
            {{form.as_p}}
            <button class="btn" type="submit">Add Item</button>
        </form>

    {% endblock %}

urls.py:

from django.urls import path
from . import views

app_name = "main"

urlpatterns = [
    path("", views.homepage, name="homepage"),
    path("register/", views.register, name="register"),
    path("logout/", views.logout_request, name="logout"),
    path("login/", views.login_request, name="login"),
    path("item_list/", views.item_list, name="items"),
    path("tender/", views.tender, name="tender"),
    path("add_item/", views.add_item, name="add_item"),


]

tables.py:

import django_tables2 as tables
from .models import Item

class ItemTable(tables.Table):
    class Meta:
        model = Item

item_list.html:

{% extends "main/header.html" %}
{% load render_table from django_tables2 %}

  {% block content %}
    <a href = "/add_item" class="waves-effect waves-light btn"><i class="material-icons left">add_circle</i>Add Item</a>
    {% render_table table %}

  {% endblock %}

stack trace:

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django\core\handlers\exception.py" in inner 34. response = get_response(request)

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django\core\handlers\base.py" in _get_response 126. response = self.process_exception_by_middleware(e, request)

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django\core\handlers\base.py" in _get_response 124. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\ASUS\mysite\main\views.py" in add_item 88. return render(request, "main/item_list.html", {'form': form})

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django\shortcuts.py" in render 36. content = loader.render_to_string(template_name, context, request, using=using)

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django\template\loader.py" in render_to_string 62. return template.render(context, request)

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django\template\backends\django.py" in render 61. return self.template.render(context)

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django\template\base.py" in render 171. return self._render(context)

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django\template\base.py" in _render 163. return self.nodelist.render(context)

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django\template\base.py" in render 937. bit = node.render_annotated(context)

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django\template\base.py" in render_annotated 904. return self.render(context)

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django\template\loader_tags.py" in render 150. return compiled_parent._render(context)

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django\template\base.py" in _render 163. return self.nodelist.render(context)

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django\template\base.py" in render 937. bit = node.render_annotated(context)

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django\template\base.py" in render_annotated 904. return self.render(context)

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django\template\loader_tags.py" in render 62. result = block.nodelist.render(context)

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django\template\base.py" in render 937. bit = node.render_annotated(context)

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django\template\base.py" in render_annotated 904. return self.render(context)

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django_tables2\templatetags\django_tables2.py" in render 145. raise ValueError("Expected table or queryset, not {}".format(klass))

Exception Type: ValueError at /add_item/ Exception Value: Expected table or queryset, not str

Any help would be appreciated

1
can you share your full stack trace?mursalin
Most probably this error is due to django-tables package. Try updating django-tables to latest version as mentioned here:stackoverflow.com/questions/29683290/…Ayush Bansal
I checked my django-tables is up to dateUmang Vira

1 Answers

0
votes

your error seems to be happening when you try to render "item_list.html".

File "C:\Users\ASUS\mysite\main\views.py" in add_item 88. return render(request, "main/item_list.html", {'form': form})

And that's the one file that you don't seem to have provided :)

Incidentally, the problem is in how you are you are using the django_tables2 library. I'm not familiar with django_tables2, but from that part of the stack trace

File "c:\users\asus\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages\django_tables2\templatetags\django_tables2.py" in render 145. raise ValueError("Expected table or queryset, not {}".format(klass))

It appears that you're passing a string to some tag that "expects a table or queryset"