gpt4 book ai didi

python - “CategoryForm”对象没有属性 'save'

转载 作者:行者123 更新时间:2023-11-30 23:22:59 25 4
gpt4 key购买 nike

使用 django 使用教程探戈(表单的乐趣),错误显示“CategoryForm”对象没有属性“保存”,请帮助项目 tango_withDjango 中有一个应用程序 range。与这里不同的是,我的程序中的标识是正确的...并且使用 ModelForm 将显示错误views.py 是

 from django.http import HttpResponse
from django.template import RequestContext
from django.shortcuts import render_to_response
from rango.models import Category
from rango.models import Page
from rango.forms import CategoryForm
def add_category(request):
# Get the context from the request.
context = RequestContext(request)


# A HTTP POST?
if request.method == 'POST':
form = CategoryForm(request.POST)

# Have we been provided with a valid form?
if form.is_valid():
# Save the new category to the database.
form.save()


# Now call the index() view.
# The user will be shown the homepage.
#return index(request)
else:
# The supplied form contained errors - just print them to the terminal.
print form.errors
else:
# If the request was not a POST, display the form to enter details.
form = CategoryForm()

# Bad form (or form details), no form supplied...
# Render the form with error messages (if any).
return render_to_response('rango/add_category.html', {'form': form}, context)
<小时/>
models.py` 
from django.db import models
class Category(models.Model):
name=models.CharField(max_length=128,unique=True)
def __unicode__(self):
return self.name

class Page(models.Model):
category=models.ForeignKey(Category)
title=models.CharField(max_length=128)
url=models.URLField()
views=models.IntegerField(default=0)
def __unicode__(self):
return self.title
<小时/>
forms.py
from django import forms
from rango.models import Page,Category
class CategoryForm(forms.Form):
name=forms.CharField(max_length=128,help_text="Please enter Category name")
views=forms.IntegerField(widget=forms.HiddenInput(),initial=0)
likes=forms.IntegerField(widget=forms.HiddenInput(),initial=0)
class Meta:
model=Category
class PageForm(forms.ModelForm) :
title=forms.CharField(max_length=128,help_text="Please enter title of the Pages")
url=forms.URLField(max_length=200,help_text="please enter url of the page")
views=forms.IntegerField(widget=forms.HiddenInput(),initial=0)

class Meta :
model=Page
fields=('title','url','views')

最佳答案

CategoryForm 应该扩展 ModelForm,而不是 Form

顺便说一句,Form 不存在 class Meta

关于python - “CategoryForm”对象没有属性 'save',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24205671/

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