我已經創建的視圖MyView的(CreateView的)和模型和形式,並得到一個錯誤:AttributeError的:對象類型「爲MyModel」有沒有屬性「_meta」
AttributeError: type object 'MyModel' has no attribute '_meta'
這是我的觀點:
class MyView(CreateView):
model = MyModel
template_name = "myapp/template1.html"
這裏是我的模型:
class MyModel(models.Model):
free_day = models.CharField(max_length=255)
name = models.CharField(max_length=255, blank=True, null=True)
class Meta:
verbose_name = _('MyModel')
verbose_name_plural = _('MyModels')
def __unicode__(self):
return self.free_day
我的形式:
class MyInputForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ('user', 'free_day',)
這是我回溯
Internal Server Error: /freedays/addvacation/
Traceback (most recent call last):
File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/db/transaction.py", line 394, in inner
return func(*args, **kwargs)
File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/views/generic/base.py", line 87, in dispatch
return handler(request, *args, **kwargs)
File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/views/generic/edit.py", line 203, in get
return super(BaseCreateView, self).get(request, *args, **kwargs)
File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/views/generic/edit.py", line 161, in get
form_class = self.get_form_class()
File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/views/generic/edit.py", line 119, in get_form_class
return model_forms.modelform_factory(model, fields=self.fields)
File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/forms/models.py", line 539, in modelform_factory
return type(form)(class_name, (form,), form_class_attrs)
File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/forms/models.py", line 284, in __new__
opts.help_texts, opts.error_messages)
File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/forms/models.py", line 184, in fields_for_model
opts = model._meta
AttributeError: type object 'MyModel' has no attribute '_meta'
請顯示* full * traceback。 – 2015-02-06 07:46:03
我加了完整的回溯。 – mark 2015-02-06 07:50:13
試用了你的代碼,它適用於我。你的'urls.py'中有什麼指向這個視圖? – 2015-02-06 08:55:48