Django-我的表单上的错误';s观点

Django-我的表单上的错误';s观点,django,forms,Django,Forms,在我的主站点(应用程序)中,我有一个名为Catalog的应用程序 我正在尝试创建一个表单来输入产品详细信息。这是第一次这样做: 在“目录文件夹”下,我有以下代码: 1) 在models.py中,我有以下模型: class Product(models.Model): name = models.CharField(max_length=255, unique=True) slug = models.SlugField(max_length=255, unique=True, he

在我的主站点(应用程序)中,我有一个名为Catalog的应用程序

我正在尝试创建一个表单来输入产品详细信息。这是第一次这样做:

在“目录文件夹”下,我有以下代码:

1) 在models.py中,我有以下模型:

class Product(models.Model):
    name = models.CharField(max_length=255, unique=True)
    slug = models.SlugField(max_length=255, unique=True, help_text='Unique value for product page URL, created from name.')
    brand = models.CharField(max_length=50)
    sku = models.CharField(max_length=50)
    price = models.DecimalField(max_digits=9,decimal_places=2)
    old_price = models.DecimalField(max_digits=9,decimal_places=2, blank=True,default=0.00)
    image = models.CharField(max_length=50)
    is_active = models.BooleanField(default=True)
    is_bestseller = models.BooleanField(default=False)
    is_featured = models.BooleanField(default=False)
    quantity = models.IntegerField()
    description = models.TextField()
    meta_keywords = models.CharField(max_length=255, help_text='Comma-delimited set of SEO keywords for meta tag')
    meta_description = models.CharField(max_length=255, help_text='Content for description meta tag')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    categories = models.ManyToManyField(Category)
    user = models.ForeignKey(User)

    class Meta:
        db_table = 'products'
        ordering = ['-created_at']

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ('catalog_product', (), { 'product_slug': self.slug })

    def sale_price(self):
        if self.old_price > self.price:
            return self.price
        else:
            return None
我用Django的DBShell在数据库中检查了这个,看起来不错

2) 在Forms.py中,我创建了

from django import forms
from CATALOG.models import Product

class Product_Form(forms.Form):
    name = forms.CharField(label='name', max_length=30)
    slug = forms.SlugField(label='Unique Name for the URL', max_length=30)
    brand = forms.CharField(label='Unique Name for the URL', max_length=30)
    price = forms.DecimalField(label='Price',max_digits=9,decimal_places=2)
    old_price = forms.DecimalField(max_digits=9,decimal_places=2, blank=True,default=0.00)
    quantity = forms.IntegerField()
    description = forms.TextField()
    meta_keywords = forms.CharField(max_length=255)
    meta_description = forms.models.CharField(max_length=255)
    categories = forms.CharField(max_length=255)
    user = forms.integerfield()

    prepopulated_fields = {'slug' : ('name',)}
3) 在views.py中,我有

# Create your views here.
from CATALOG.forms import *
def enter_product(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = User.objects.create_user(
                username=form.clean_data['username'],
                password=form.clean_data['password1'],
                email=form.clean_data['email']
            )
            return HttpResponseRedirect('/')
        else:
            form = RegistrationForm()
        variables = RequestContext(request, {
            'form': form
        })

        return render_to_response(
            'CATALOG/enter_product.html',
            variables
        )
4) 在URL.py中

from django.conf.urls.defaults import *
from CATALOG.views import *

urlpatterns = patterns('SOWL.catalog.views',
    (r'^$', 'index', { 'template_name':'catalog/index.html'}, 'catalog_home'),
    (r'^category/(?P<category_slug>[-\w]+)/$', 'show_category', {'template_name':'catalog/category.html'},'catalog_category'),
    (r'^product/(?P<product_slug>[-\w]+)/$', 'show_product', {'template_name':'catalog/product.html'},'catalog_product'),
    (r'^enter_product/$',enter_product),
)
从django.conf.url.defaults导入*
从CATALOG.views导入*
urlpatterns=patterns('SOWL.catalog.views',
(r“^$”、“index”、{“template_name”:“catalog/index.html”}、“catalog_home”),
(r“^category/(?P[-\w]+)/$”、“show_category”、{“template_name”:“catalog/category.html”}、“catalog_category”),
(r“^product/(?P[-\w]+)/$”、“show_product”、{“template_name”:“catalog/product.html”}、“catalog_product”),
(r“^输入产品/$”,输入产品),
)
我已经创建了名为views.py的Temaplate

但我得到了这个错误

init()获得意外的关键字参数“default”

这实际上是指向旧的价格变量

Environment:


Request Method: GET
Request URL: http://localhost:8000/

Django Version: 1.4
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.humanize',
 'CATALOG',
 'SOWLAPP',
 'registration',
 'django.contrib.admin',
 'django.contrib.admindocs')
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')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  101.                             request.path_info)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
  298.             for pattern in self.url_patterns:
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in url_patterns
  328.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in urlconf_module
  323.             self._urlconf_module = import_module(self.urlconf_name)
File "C:\Python27\lib\site-packages\django\utils\importlib.py" in import_module
  35.     __import__(name)
File "C:\SHIYAM\Personal\SuccessOwl\SOWL0.1\SOWL\SOWL\urls.py" in <module>
  4. from CATALOG.views import *
File "C:\SHIYAM\Personal\SuccessOwl\SOWL0.1\SOWL\CATALOG\views.py" in <module>
  2. from CATALOG.forms import *
File "C:\SHIYAM\Personal\SuccessOwl\SOWL0.1\SOWL\CATALOG\forms.py" in <module>
  13. class Product_Form(forms.Form):
File "C:\SHIYAM\Personal\SuccessOwl\SOWL0.1\SOWL\CATALOG\forms.py" in Product_Form
  18.   old_price = forms.DecimalField(max_digits=9,decimal_places=2, blank=True,default=0.00)
File "C:\Python27\lib\site-packages\django\forms\fields.py" in __init__
  272.         Field.__init__(self, *args, **kwargs)

Exception Type: TypeError at /
Exception Value: __init__() got an unexpected keyword argument 'default'
环境:
请求方法:获取
请求URL:http://localhost:8000/
Django版本:1.4
Python版本:2.7.3
已安装的应用程序:
(“django.contrib.auth”,
“django.contrib.contenttypes”,
“django.contrib.sessions”,
“django.contrib.sites”,
“django.contrib.messages”,
“django.contrib.staticfiles”,
“django.contrib.humanize”,
“目录”,
“SOWLAPP”,
“注册”,
“django.contrib.admin”,
'django.contrib.admindocs')
已安装的中间件:
('django.middleware.common.CommonMiddleware',
“django.contrib.sessions.middleware.SessionMiddleware”,
“django.middleware.csrf.CsrfViewMiddleware”,
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
回溯:
get\U响应中的文件“C:\Python27\lib\site packages\django\core\handlers\base.py”
101请求路径(信息)
解析中的文件“C:\Python27\lib\site packages\django\core\urlresolvers.py”
298对于self.url_模式中的模式:
url\u模式中的文件“C:\Python27\lib\site packages\django\core\urlresolvers.py”
328patterns=getattr(self.urlconf_模块,“urlpatterns”,self.urlconf_模块)
urlconf_模块中的文件“C:\Python27\lib\site packages\django\core\urlresolvers.py”
323self.\u urlconf\u module=import\u模块(self.urlconf\u名称)
导入模块中的文件“C:\Python27\lib\site packages\django\utils\importlib.py”
35.     __导入(名称)
文件“C:\SHIYAM\Personal\SuccessOwl\SOWL0.1\SOWL\SOWL\url.py”
4.从CATALOG.views导入*
文件“C:\SHIYAM\Personal\SuccessOwl\SOWL0.1\SOWL\CATALOG\views.py”
2.从CATALOG.forms导入*
文件“C:\SHIYAM\Personal\SuccessOwl\SOWL0.1\SOWL\CATALOG\forms.py”
13类别产品表格(表格.表格):
产品表单中的文件“C:\SHIYAM\Personal\SuccessOwl\SOWL0.1\SOWL\CATALOG\forms.py”
18old_price=forms.DecimalField(最大位数=9,小数位数=2,空格=True,默认值=0.00)
\uuu init中的文件“C:\Python27\lib\site packages\django\forms\fields.py”__
272字段.\uuuuu初始化(self,*args,**kwargs)
异常类型:位于的TypeError/
异常值:\uuuu init\uuuuu()获取了意外的关键字参数“default”
我被困在这里了)非常感谢你的帮助

  • 多伦多

    • 在您的
      产品表单中

      old_price = forms.DecimalField(max_digits=9,decimal_places=2, blank=True,default=0.00)
      

      default=0.00
      更改为
      initial=0.00
      default
      关键字用于模型,而
      initial
      关键字用于表单。

      产品表单中

      old_price = forms.DecimalField(max_digits=9,decimal_places=2, blank=True,default=0.00)
      
      default=0.00
      更改为
      initial=0.00
      default
      关键字用于模型,而
      initial
      关键字用于表单