2

私のメインサイト(アプリケーション)には、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'

私はここで立ち往生しています。:(。どんな助けでも大歓迎です。

  • トロント
4

1 に答える 1

4

あなたの中でProduct_Form

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

に変更default=0.00initial=0.00ます。defaultキーワードはモデルとinitialフォームに使用されます。

于 2012-09-12T21:44:19.677 に答える