0

我是 Django 新手,我正在尝试创建我的第一个简单应用程序。

我想试试createView,但是在尝试调用相应的模板时总是报错。

这是错误:

Environment:


Request Method: GET
Request URL: http://192.168.2.55:8000/colist/paymentNew/

Django Version: 1.6.2
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'polls',
 'colist')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in dispatch
  87.         return handler(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/edit.py" in get
  201.         return super(BaseCreateView, self).get(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/edit.py" in get
  159.         form_class = self.get_form_class()
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/edit.py" in get_form_class
  118.             return model_forms.modelform_factory(model, fields=self.fields)
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in modelform_factory
  528.     return type(form)(class_name, (form,), form_class_attrs)
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in __new__
  282.                                       opts.help_texts, opts.error_messages)
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in fields_for_model
  175.     opts = model._meta

Exception Type: AttributeError at /colist/paymentNew/
Exception Value: type object 'Payment' has no attribute '_meta'

这是我的models.py

from django.db import models

# Create your models here.

class User(models.Model):
    firstName = models.CharField(max_length=50)
    lastName = models.CharField(max_length=50)
    picture = models.ImageField(upload_to='pics/', default='pics/test.jpg')
    email = models.EmailField()
    def __unicode__(self):
            return self.firstName + " " + self.lastName

class Bill(models.Model):
    user = models.ForeignKey(User)
    amount = models.DecimalField(max_digits=8, decimal_places=2)

class Payment(models.Model):
    user = models.ForeignKey(User)
    amount = models.DecimalField(max_digits=8, decimal_places=2)
    newBalance = models.DecimalField(max_digits=8, decimal_places=2)
    date = models.DateTimeField('payment date')    

来自views.py的片段

[...other stuff...]

class Payment(ListView):
    model = Payment
    template_name = 'colist/payment.html' 

class PaymentNew(CreateView):
    model = Payment
    template_name = 'colist/paymentNew.html' 

    def get_success_url(self):
        return reverse('colist:paymentNew')

和模板 paymentNew.html

{% load staticfiles %}

<body>

<h1>Payments</h1>

<ul>
    {% for payment in object_list %}
        <li class="payment">
            {{ payment.user.firstName }}  {{payment.user.lastName}}   {{payment.amount}} EUR   {{payment.date}}
        </li>
    {% endfor %}
</ul>


<form action="{% url 'colist:paymentNew' %}" method="POST">
    {% csrf_token %}
    <ul>
        {{ form.as_ul }}
    </ul>
    <input id="save_payment" type="submit" value="Save" />
</form>
<a href= "{% url "colist:payment" %}"> back to list </a>

</body>

我不知道“_meta”属性应该来自哪里。我还注意到,在处理此错误时:如果我在views.py中更改模型“Bill”而不是“Payment”模型,如下所示:

class PaymentNew(CreateView):
    model = Bill
    template_name = 'colist/paymentNew.html' 

    def get_success_url(self):
        return reverse('colist:paymentNew')

为什么它与账单而不是付款一起使用?我看不出两者有什么大的区别。

谢谢您的帮助!

4

0 回答 0