gpt4 book ai didi

Django - 我的表单 View 出错

转载 作者:行者123 更新时间:2023-12-04 18:12:10 26 4
gpt4 key购买 nike

在我的主站点(应用程序)中,我有一个名为 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) 在 URLs.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),
)

我创建了在views.py 中调用的模板。

但我收到了这个错误。

初始化 () 得到了一个意外的关键字参数“默认”

这实际上是指向 old_price 变量。
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'

我被困在这里。 :(。 任何帮助深表感谢。
  • 多伦多
  • 最佳答案

    在您的 Product_Form

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

    更改 default=0.00initial=0.00 . default关键字用于模型, initial对于表格。

    关于Django - 我的表单 View 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12396427/

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