我是 Django 的新手。我在下面给出了我的代码。任何人都可以帮助我吗?我找不到错误在哪里。提前致谢。
addnewcus.html
<form id="addnewcus_form" method="POST" action=""> {% csrf_token %}
<ul>
<li id="li_1">
<label class="description" for="id_bname">Customer Name </label>
<div> {{ form.cname }} </div>
</li>
<li id="li_2">
<label class="description" for="id_bauthor">Customer Address </label>
<div> {{ form.caddress }} </div>
</li>
</ul>
</form>
模型.py
from django.db import models
class cusdetails(models.Model):
cid=models.IntegerField()
cname=models.CharField(max_length=100)
caddress=models.CharField(max_length=100)
active=models.BooleanField(default=True)
网址.py
urlpatterns = patterns('',url(r'^addnewcus/$', 'cusapp.views.addnewcus_view', name='url_addnewcus'),)
表格.py
import random
def get_rand_no():
return random.randint(1234567, 9999999)
class addnewcus_form(ModelForm):
cname = forms.CharField(widget=forms.TextInput())
caddress = forms.CharField(widget=forms.TextInput())
class Meta:
model = bookdetails
exclude = ('cid','active')
def save_cusdetails(self):
cusdetail_data = self.cleaned_data
randomnumber = get_rand_no()
cusdetails_createobj = cusdetails.objects.create(cid=randomnumber, cname=cusdetail_data['cname'], caddress=cusdetail_data['caddress'],active=True)
return cusdetails_createobj
视图.py
def addnewcus_view(request, template_name="addnewcus.html"):
if request.method=="POST":
cusdetail_data=addnewcus_form(data=request.POST)
if cusdetail_data.is_valid():
addcusdetail_obj = cusdetail_data.save_cusdetails()
return HttpResponseRedirect(reverse("url_index"))
else:
return render(request, template_name,{'form':cusdetail_data})
elif request.method=="GET":
cusdetail_data=addnewcus_form()
return render(request, template_name,{'form':bookdetail_data})
当我点击网址时,它显示以下错误。
'addnewcus_form' 对象在 django 中没有属性 'save_cusdetails' 错误