gpt4 book ai didi

django - ModelForm 类 Contact 没有属性 '_meta'

转载 作者:行者123 更新时间:2023-12-01 09:32:57 49 4
gpt4 key购买 nike

当我在我的 ContactForm 中继承 ModelForm 时,我收到此错误,当我有它时,modelform 没有从 modelform 继承没有错误,只是在 html 页面上没有表单。我真的想不通这个

/addacontact 处的属性错误
类 Contact 没有属性“_meta”

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/addacontact

Django Version: 1.4.2
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'south',
'sekizai')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/core/handlers/base.py" in get_response
89. response = middleware_method(request)
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/middleware/common.py" in process_request
67. if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/core/urlresolvers.py" in is_valid_path
531. resolve(path, urlconf)
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/core/urlresolvers.py" in resolve
420. return get_resolver(urlconf).resolve(path)
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/core/urlresolvers.py" in resolve
298. for pattern in self.url_patterns:
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/core/urlresolvers.py" in url_patterns
328. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/core/urlresolvers.py" in urlconf_module
323. self._urlconf_module = import_module(self.urlconf_name)
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/utils/importlib.py" in import_module
35. __import__(name)
File "/home/brian/projects/steprider/steprider/urls.py" in <module>
2. from steprider.views import Add_a_contact
File "/home/brian/projects/steprider/steprider/views.py" in <module>
9. from salesflow.forms import *
File "/home/brian/projects/steprider/salesflow/forms.py" in <module>
13. class ContactForm(ModelForm):
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/forms/models.py" in __new__
206. opts.exclude, opts.widgets, formfield_callback)
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/forms/models.py" in fields_for_model
146. opts = model._meta

Exception Type: AttributeError at /addacontact
Exception Value: class Contact has no attribute '_meta'

这是forms.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# steprider.salesflow.forms.py
#
# Copyright 2012 BRIAN SCOTT CARPENTER <KlanestroTalisman@gmail.com>

from django.forms import ModelForm
from salesflow import models
from django import forms
from django.template.defaultfilters import slugify

class ContactForm(ModelForm):
description = forms.CharField(widget=forms.Textarea)

class Meta:
model = models.Contact
exclude = ("user","slug")

def save(self, user):
contact = super(ContactForm, self).save(commit=False)
contact.user = ser
contact.save()
return contact

class Contact_History_Form:

class Meta:
model = models.Contact_History
exclude = ("user","slug")

模型.py
from django.db import models
from django.contrib.auth.models import User
from django_extensions.db.fields import AutoSlugField



# Create your models here.
class Contact:
user = models.ForeignKey(User)
business = models.CharField(max_length=50)
title = models.CharField(max_length=50)
name = models.CharField(max_length=50)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
email = models.EmailField()
description = models.CharField(max_length=3000)
slug = models.SlugField(max_length=128)
slug = AutoSlugField(('slug'), max_length=128, unique=True, populate_from=('business',))


def __unicode__(self):
return self.slug


class Contact_History:
user = models.ForeignKey(User)
contact = models.ForeignKey('Contact')

number_of_emails_sent = models.IntegerField()
last_email_sent = models.DateField()

phone_notes = models.CharField(max_length=2000)


slug = models.SlugField(max_length=128)
slug = AutoSlugField(('slug'), max_length=128, unique=True, populate_from=('name',))


def __unicode__(self):
return self.slug

最佳答案

您需要继承 models.Model 对于您的模型类:

class Contact:
...

应该:
class Contact(models.Model):
...
models.Model_meta属性,其中 Contact模型将继承并在生成时使用 ModelForm .

关于django - ModelForm 类 Contact 没有属性 '_meta',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12994539/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com