Django,Materialize,Django-tables2:期望的表或查询集,而不是str

问题描述 投票:0回答:1

我有一个表单,并且其中一个字段是MultipleChoiceField。表单使用Materialize以模态呈现。我在用表格呈现选择时遇到问题,但发现此answer

我将字段更改为:

comp = forms.MultipleChoiceField(choices=COMP, widget=forms.Select(choices=COMP, attrs={'class': 'browser-default'}))

解决了渲染问题。

一旦提交表单,我希望可以查看django-tables2表中的数据,该表工作正常,但是现在我收到以下错误:

异常值:期望的表或查询集,而不是str

如果删除窗口小部件,表单将无法呈现,但是我可以提交表单并查看表中的数据。

COMP是字段选择序列。

COMP = (
    ('Daily', 'Daily'),
    ('Weekly', 'Weekly'),
    ('Monthly', 'Monthly'),
    ('Quarterly', 'Quarterly'),
    ('Yearly', 'Yearly'),
)

完整回溯

Traceback (most recent call last):
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\Gary\Desktop\mysite\loan\views.py", line 119, in post
    return render(request, self.template_name, {'form': form})
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 171, in render
    return self._render(context)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\Gary\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django_tables2\templatetags\django_tables2.py", line 145, in render
    raise ValueError("Expected table or queryset, not {}".format(klass))

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

forms.py

from django import forms
from loan.models import Debtor, DebtorTransaction

COMP = (
    ("Monthly", "Monthly"),
    ("Quarterly", "Quarterly"),
    ("Half Yearly", "Half Yearly"),
    ("Yearly", "Yearly"),
)


class DebtorForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(DebtorForm, self).__init__(*args, **kwargs)
        self.fields['comp'].label = "Compounding Period"

    comp = forms.MultipleChoiceField(choices=COMP, widget=forms.SelectMultiple(choices=COMP, attrs={'class': 'browser-default'}))

    class Meta:
        model = Debtor
        widgets = {
            'courtdate': forms.DateInput(attrs={'class': 'datepicker'}),
        }

        fields = '__all__'
        exclude = [
            'status',
            'account',
            'loan',
            'number',
            'unique']


class DebitTransactionForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.id = kwargs.pop('debtor', [])
        super(DebitTransactionForm, self).__init__(*args, **kwargs)

    class Meta:
        model = DebtorTransaction
        widgets = {
        }
        fields = '__all__'
        exclude = [
            'debtor',
            'date',
            'type',
            'initiationfee',
            'transaction_balance',
            'interest',
            'rate']


class CreditTransactionForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.id = kwargs.pop('debtor', [])
        super(CreditTransactionForm, self).__init__(*args, **kwargs)

    class Meta:
        model = DebtorTransaction
        widgets = {
        }
        fields = '__all__'
        exclude = [
            'debtor',
            'date',
            'type',
            'initiationfee',
            'transaction_balance',
            'interest',
            'rate']


class InitiationForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.id = kwargs.pop('debtor', [])
        super(InitiationForm, self).__init__(*args, **kwargs)

    class Meta:
        model = DebtorTransaction
        widgets = {
        }
        fields = '__all__'
        exclude = [
            'amount',
            'description',
            'debtor',
            'date',
            'type',
            'transaction_balance',
            'interest',
            'rate']

tables.py

import django_tables2 as tables
from .models import Debtor, DebtorTransaction
from django_tables2.utils import A


def balance_footer(table):
    try:
        s = sum(x.debtoraccount.balance for x in table.data)
    except Exception as e:
        raise

    return '{:,}'.format(s).replace(',', ' ')


class DebtorsTable(tables.Table):
    balance = tables.Column(accessor='debtoraccount.balance', footer=balance_footer,
                            attrs={"td": {'style': 'text-align: right;'},
                                   "th": {'style': 'text-align: right;'}})
    courtdate = tables.DateColumn(verbose_name='Court Date', format='d F Y',
                                  attrs={"td": {'style': 'text-align: center;'},
                                         "th": {'style': 'text-align: center;'}})
    LMS = tables.Column(attrs={"td": {'style': 'text-align: center;'}, "th": {'style': 'text-align: center;'}})
    link = tables.LinkColumn('loan:debtors_with_pk', text='Open', args=[A('pk')], orderable=False, empty_values=(),
                             verbose_name='')

    def render_balance(self, value):
        return '{:,}'.format(value).replace(',', ' ')

    class Meta:
        model = Debtor
        template_name = "django_tables2/bootstrap.html"
        fields = ("name", "LMS", "courtdate")
        sequence = ("link", "name", "LMS", "courtdate", "balance")


class TransactionTable(tables.Table):
    transaction_balance = tables.Column(verbose_name='Balance', attrs={"td": {'style': 'text-align: right;'},
                                                                       "th": {'style': 'text-align: right;'}})
    amount = tables.Column(verbose_name='Amount', attrs={"td": {'style': 'text-align: right;'},
                                                         "th": {'style': 'text-align: right;'}})
    type = tables.Column(verbose_name='', attrs={"td": {'style': 'text-align: center;'},
                                                 "th": {'style': 'text-align: center;'}})
    date = tables.DateColumn(verbose_name='Date', format='d F Y', attrs={"td": {'style': 'text-align: center;'},
                                                                         "th": {'style': 'text-align: center;'}})

    def render_amount(self, value, record):
        if record.type == DebtorTransaction.CREDIT:
            return '- {:,}'.format(value).replace(',', ' ')
        else:
            return '{:,}'.format(value).replace(',', ' ')

    def render_transaction_balance(self, value):
        return '{:,}'.format(value).replace(',', ' ')

    class Meta:
        model = DebtorTransaction
        template_name = "django_tables2/bootstrap.html"
        fields = ("date", "description", "amount", "type", "transaction_balance")

views.py

from django.shortcuts import render, redirect
from django.views.generic import TemplateView
from loan.forms import DebtorForm, DebitTransactionForm, CreditTransactionForm, InitiationForm
from loan.models import Debtor, DebtorAccount, DebtorTransaction
from loan.tables import DebtorsTable, TransactionTable
from django_tables2 import RequestConfig
from decimal import Decimal


class DebtorView(TemplateView):
    template_name = 'loan/skuld.html'

    def get(self, request, pk=None):
        model = Debtor, DebtorTransaction
        form1 = DebitTransactionForm()
        form2 = CreditTransactionForm()
        form3 = InitiationForm()
        debtor = Debtor.objects.get(pk=pk)
        debtor_id = debtor.id

        table2 = TransactionTable(DebtorTransaction.objects.all().filter(debtor_id=debtor_id))
        RequestConfig(request).configure(table2)

        args = {'form1': form1, 'form2': form2, 'form3': form3, 'debtor': debtor, 'table2': table2}

        return render(request, self.template_name, args)

    def post(self, request, pk=None):
        args = {}
        if request.method == 'POST':
            debtor = DebtorAccount.objects.get(pk=pk)
            form1 = DebitTransactionForm(request.POST, debtor)
            form2 = CreditTransactionForm(request.POST, debtor)
            form3 = InitiationForm(request.POST, debtor)
            success = False

            if 'debit_button' in request.POST and form1.is_valid() and form3.is_valid():
                publish1 = form1.save(commit=False)
                publish1.debtor = DebtorAccount.objects.get(pk=pk)
                publish1.type = 'DEBIT'
                publish1.save()
                publish3 = form3.save(commit=False)
                form1 = DebitTransactionForm()
                publish3.debtor = DebtorAccount.objects.get(pk=pk)
                if publish3.initiationfee is True:
                    skuldenaar = publish1.amount

                    def get_initiationfee(skuldenaar):
                        if publish3.initiationfee is True:
                            if skuldenaar >= Decimal('8850'):
                                initiation = Decimal('1050')
                            else:
                                initiation = (skuldenaar * Decimal('0.1')) + Decimal('165')
                        else:
                            initiation = Decimal('0')

                        return(initiation)

                    publish3.amount = get_initiationfee(skuldenaar)
                    publish3.description = 'Initiation Fee'
                    publish3.type = 'INITIATION'
                    publish3.save()
                    form3 = InitiationForm()
                success = True

            if 'credit_button' in request.POST and form2.is_valid():
                publish2 = form2.save(commit=False)
                publish2.debtor = DebtorAccount.objects.get(pk=pk)
                publish2.type = 'CREDIT'
                publish2.save()
                form2 = DebitTransactionForm()
                success = True

            if success:
                return redirect('loan:debtors_with_pk', pk=pk)

        else:
            debtor = DebtorAccount.objects.get(pk=pk)
            form1 = DebitTransactionForm(request.POST, debtor)
            form2 = CreditTransactionForm(request.POST, debtor)
            form3 = InitiationForm(request.POST, debtor)

        args['form1'] = form1
        args['form2'] = form2
        args['form3'] = form3

        return render(request, self.template_name, args)

    def success(request):
        return render(request, 'loan/skuld.html', {})


class AllDebtorsView(TemplateView):
    template_name = 'loan/debtors.html'

    def get(self, request, pk=None):
        model = Debtor
        form = DebtorForm()
        debtors = Debtor.objects.all()
        table1 = DebtorsTable(Debtor.objects.all().filter())
        RequestConfig(request).configure(table1)

        field_names = [f.name for f in model._meta.get_fields()]

        data = [[getattr(ins, name) for name in field_names]
                for ins in Debtor.objects.prefetch_related().all()]

        args = {'form': form, 'field_names': field_names, 'data': data, 'debtors': debtors, 'table1': table1}

        return render(request, self.template_name, args)

    def post(self, request):
        form = DebtorForm(request.POST)
        if form.is_valid():
            form.save()
            form = DebtorForm
            return redirect('/debtors')
        return render(request, self.template_name, {'form': form})

模板

{% extends 'loan/base.html' %}
{% load render_table from django_tables2 %}

<html lang="en">
        {% block title %}
            All Debtors
        {% endblock %}

        {% block body %}
            <div id="modal1" class="modal">
                <div class="modal-content">
                    <h4>New Debtor</h4><br>
                    <form method="post">
                        {% csrf_token %}
                        {{ form.as_p }}
                        <div class="modal-footer">
                            <button type="submit">Save</button>
                        </div>
                    </form>
                </div>
            </div>
            <div class="container">
                <div class="fixed-action-btn">
                    <a class="btn-floating btn-large waves-effect waves-light blue darken-1 btn modal-trigger" href="#modal1"">
                        <i class="large material-icons">person_add</i>
                    </a>
                </div>
            </div><br>
            <div class="container">
                <h5>Debtors</h5>
                {% render_table table1 %}
            </div>

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>

            <script>
                jQuery(document).ready(function($) {
                    $(".clickable-row").click(function() {
                  window.location = $(this).data('url');
                    });
                });
            </script>

            <script>
                $(document).ready(function(){
                    $('.fixed-action-btn').floatingActionButton();
                });
            </script>

            <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>

            <script>
                $(document).ready(function(){
                    $('.modal').modal();
                });
            </script>

            <script>
                $(document).ready(function(){
                    $('.datepicker').datepicker({format: 'yyyy-mm-dd', firstDay: 1});
                });
            </script>
        {% endblock %}
</html>

我想念的是什么?我该如何解决?

[请让我知道是否应该发布其他代码,谢谢

django django-tables2
1个回答
0
投票

我按照@schillingt的建议安装了django-debug-toolbar。每次删除该小部件时,该表都会呈现,但是当我用以下行运行它时,任何内容都不会传递给render_table

comp = forms.MultipleChoiceField(choices=COMP, widget=forms.Select(choices=COMP, attrs={'class': 'browser-default'}))

然后我遇到了以下answer。并阅读了documents

根据文档,MultipleChoiceField归一化为字符串列表,而ChoiceField归一化为单个字符串。我将MultipleChoiceField更改为ChoiceField,它起作用了,提交了表单并呈现了表格。

我不完全了解更改的技术性,但是我假设每次我从上一行删除窗口小部件时,用于选择一个选项的表单输入都会消失,并且使用了模型字段的默认值,并且已呈现了表格。

我错误地认为这是导致问题的小部件。

我的理解是,表单中的MultipleChoiceField创建了一个字符串列表,并将其传递给期望一个字符串的模型。规范化为单个字符串的ChoiceField解决了该问题。

以下内容对我有用:

comp = forms.ChoiceField(choices=COMP, widget=forms.Select(attrs={'class': 'browser-default'}))
© www.soinside.com 2019 - 2024. All rights reserved.