2012-09-12 65 views
2

在我的主站點(應用程序)中,我有一個名爲Catalog的應用程序。Django - 我窗體視圖中的錯誤

我正在嘗試創建一個表單來輸入產品詳細信息。這是第一次這樣做:

在目錄文件夾中,我有以下代碼:

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',)} 

在views.py 3),I具有

# 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()得到了意想不到的關鍵字參數 '默認'

這實際上是實際指向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' 

我被困在這裏。 :(。任何幫助深表感謝。

  • 多倫多

回答

4

Product_Form

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

變化default=0.00initial=0.00default關鍵字用於模型,initial的形式。