属性错误:“ WSGIRequest”对象没有属性“ get”

时间:2020-04-09 18:05:57

标签: python django

我最近一直在从事一个项目,并遇到了上述错误。它说“模板渲染期间出错”。我有一个类似的模型,可以很好地工作。我一直在寻找类似的错误,但没有一个适合我的情况。我不知道我哪里出了错。如果我得到有用的答案,那就太好了。

Models.py

class ServiceTax(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE,related_name="service_tax",null=True,blank=True)
    name=models.CharField(max_length=100)
    percent=models.FloatField(default='0')
    add_amount=models.IntegerField(default='0')

    def __str__(self):
        return self.name

Forms.py

class ServiceTaxForm(forms.ModelForm):
class Meta:
    model = ServiceTax
    fields = "__all__"
    widgets = {
        'name' : forms.TextInput(attrs={'class': 'form-control'}),
        'percent' : forms.NumberInput(attrs={'class': 'form-control','step':'0.01'}),
        'add_amount' : forms.NumberInput(attrs={'class':'form-control','maxlength':5}),


    }
    labels={
        'add_amount': "Additional Amount"
    }

Views.py

def tax_form(request,id=0):
if request.method == 'GET':
    if id == 0:
        form = ServiceTaxForm(request)
    else:

        tax = ServiceTax.objects.get(pk=id)
        if tax in request.user.service_tax.all():
            form = ServiceTaxForm(request,instance=tax)
        else:
            return redirect('/revenue/tax')
    return render(request,'tax-form.html',{'form':form})
else:
    if id==0:
        form = ServiceTaxForm(request,request.POST)
        if form.is_valid():



            name = form.cleaned_data["name"]

            percent = form.cleaned_data["percent"]
            add_amount = form.cleaned_data["add_amount"]




            t = AnnualTax(
                name=name,
                percent=percent,
                add_amount=add_amount,

                )

            t.save()
            request.user.service_tax.add(t)
    else:

        tax = ServiceTax.objects.get(pk=id)
        if tax in request.user.service_tax.all():
            form = ServiceTaxForm(request,request.POST,instance=tax)
            if form.is_valid():

                name = form.cleaned_data["name"]

                percent = form.cleaned_data["percent"]
                add_amount = form.cleaned_data["add_amount"]



                tax_obj = ServiceTax.objects.get(pk=id)
                tax_obj.name = name


                tax_obj.percent = percent
                tax_obj.add_amount = add_amount

                tax_obj.save()


    return redirect('/revenue/tax')

tax-form.html

{% extends 'base.html' %}
{% load crispy_forms_tags %}

{% block content %}
<a href="{% url 'tax_list' %}"><i class="fa fa-chevron-circle-left fa-3x m-2"></i></a>
<div class="content">
<div class="container-fluid">
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header">
                    <h4 class="card-title">Add Service Tax</h4>
                </div>
                <div class="card-body">
                    <form action="" method="POST" autocomplete="off">
                        {% csrf_token %}

                        <div class="row">
                            <div class="col-md-4 pr-1">
                                <div class="form-group">

                                    {{ form.name | as_crispy_field}}
                                </div>
                            </div>
                            <div class="col-md-4 pr-1">
                                <div class="form-group">

                                    {{ form.percent | as_crispy_field}}
                                </div>
                            </div>
                            <div class="col-md-4 pr-1">
                                <div class="form-group">

                                    {{ form.add_amount | as_crispy_field}}
                                </div>
                            </div>
                        </div>

                        <button type="submit" class="btn btn-success btn-fill pull-right">
                            {% if request.get_full_path == '/income/tax/add/' %}
                            Add Tax
                            {% else %}
                            Update
                            {% endif %}
                        </button>



                    </form>
                </div>
            </div>
        </div>

    </div>
</div>
</div>
{% endblock %}

1 个答案:

答案 0 :(得分:0)

您应该检索通过 request.METHOD 的表单,而不仅仅是请求

作为示例,这段代码form = ServiceTaxForm(request)应该为form = ServiceTaxForm(request.POST)