我是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>
我不知道&#34; _meta&#34;属性应该来自。 另外我注意到,在玩这个错误的时候:如果我在views.py中为模型更改了模型&#34; Bill&#34;而不是&#34;付款&#34;像这样:
class PaymentNew(CreateView):
model = Bill
template_name = 'colist/paymentNew.html'
def get_success_url(self):
return reverse('colist:paymentNew')
为什么它与Bill合作而不是付款?我不认为两者之间有任何重大差异。
感谢您的帮助!