在我的主站点(应用程序)中,我有一个名为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中调用的Temaplate。
但是我收到了这个错误。
init ()获得了意外的关键字参数'default'
实际上实际上是指向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'
我被困在这里。 :(。非常感谢任何帮助。
答案 0 :(得分:4)
在Product_Form
old_price = forms.DecimalField(max_digits=9,decimal_places=2, blank=True,default=0.00)
将default=0.00
更改为initial=0.00
。 default
关键字用于模型,initial
用于表单。