/service 'Orderbook' 对象的 AttributeError 没有属性 'save'。 , 保存是一个函数 fm.save()

AttributeError at /service 'Orderbook' object has no attribute 'save'. , save is a function fm.save()

得到一个属性错误,将函数显示为错误或属性。 我曾尝试解决它但它不起作用我删除了所有迁移的文件并再次 运行 python manage.py makemigrations 和 migrate 命令但仍然显示相同的错误 admin.py

中的代码
# Register your models here.
from django.contrib import admin
from .models import *
# Register your models here.
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    list_display=('user_id','username','password','name','email_id','contact','address')

@admin.register(Orders)
class OrdersAdmin(admin.ModelAdmin):
    list_display=('oid','parcel_info','parcel_qty','parcel_weight','unit_mass','desti')

views.py

中的代码
from django.shortcuts import redirect, render
from django.contrib.auth import authenticate,login
from user.forms import *
from django.contrib import messages
from user.models import *

from user.models import Orders

# Create your views here.
def home(request):
    return render(request,'home.html')

def login(request):
    if request.method=='POST':
        username=request.POST['username']
        password=request.POST['password']
        userm=authenticate(user=username,passe=password)
        if userm is not None:
            login(request,userm)
            messages.success(request,"You have login successfully...")
            return redirect("home")
        else:
            messages.error(request,"Invalid Username or password...")
            return redirect("login")
    else:
        return render(request,'base.html')

def register(request):
    if request.method=='POST':
        fm=Userregistration(request.POST)
        if fm.is_valid():
            fm.save()
    else:
        fm=Userregistration()
        return render(request,'register.html',{'form':fm})

def Orderreg(request):
    if request.method=='POST':
        fm=Orderbook(request.POST)
        if fm.is_valid():
            fm.save()
            fm=Orderbook()
        return redirect('service')
    else:
        fm=Orderbook()
        u=Orders.objects.all()
    return render(request,'service.html',{'form':fm,'us':u})

def contact(request):
    return render(request,'contact.html')


def about(request):
    return render(request,'about.html')

HTML代码

{% extends 'base.html' %}
{% block body %}
{% load static %}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="{% static 'css/bootstrap.css' %}">
    </head>
    <body>
        <form action="" method="POST">
            {% csrf_token %}
            <section class="" style="background-color: #b6977d;">
                <div class="container h-200">
                  <div class="row d-flex justify-content-center align-items-center h-100">
                    <div class="col-lg-12 col-xl-11">
                      <div class="card text-black my-5" style="border-radius: 35px;">
                        <div class="card-body p-md-5">
                          <div class="row justify-content-center">
                            <div class="col-md-10 col-lg-6 col-xl-5 order-2 order-lg-1">
              
                              <p class="text-center h1 fw-bold mb-5 mx-1 mx-md-4 mt-4">Place order</p>
                                
                              {{form.as_p}}
                              <input type="submit" class="btn btn-success btn-block" value="Order">
                            </div>
                            <div class="col-md-10 col-lg-6 col-xl-7 d-flex align-items-center order-1 order-lg-2">
              
                              <img src="/static/user-registration-removebg-preview.png" class="img-fluid" alt="Sample image">
              
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </section>
        <script src="{% static 'js/jquery.js' %}"></script>
        <script src="{% static 'js/poper.js' %}"></script>
        <script src="{% static 'js/bootstrap.js' %}"></script>
    </body>
</html>

{% endblock body %}

forms.py

中的代码
from django import forms
from user.models import *

class Userregistration(forms.ModelForm):
    class Meta:
        model=User
        fields=['username','password','name','email_id','contact','address']
        widgets={
            'username':forms.TextInput(attrs={'class':'form-control'}),
            'password':forms.PasswordInput(attrs={'class':'form-control'}),
            'name':forms.TextInput(attrs={'class':'form-control'}),
            'email_id':forms.EmailInput(attrs={'class':'form-control'}),
            'contact':forms.TextInput(attrs={'class':'form-control'}),
            'address':forms.TextInput(attrs={'class':'form-control'}),
        }

class Orderbook(forms.Form):
    parcel_info=forms.CharField()
    parcel_qty=forms.DecimalField()
    parcel_weight=forms.DecimalField()
    unit_mass=forms.CharField()
    desti=forms.CharField()

models.py

中的代码
from django.db import models

# Create your models here.
class User(models.Model):
    user_id = models.IntegerField(primary_key=True)
    username = models.CharField(max_length=20)  
    password = models.CharField(max_length=15)  
    name = models.CharField(max_length=20)  
    email_id = models.EmailField(max_length=100) 
    contact = models.IntegerField(default=1234567890)
    address = models.CharField(max_length=30) 
    
class Orders(models.Model):
    oid = models.IntegerField(primary_key=True)
    parcel_info = models.CharField(max_length=100)
    parcel_qty=models.IntegerField(default=1)
    parcel_weight=models.IntegerField(default=1.5)
    unit_mass=models.CharField(max_length=10,default="kg")
    desti=models.CharField(max_length=100)

urls.py

中的代码
from django.urls.conf import path
from user import views


urlpatterns = [
    path('',views.home,name='home'),
    path('home',views.home,name='home'),
    path('login',views.login,name='login'),
    path('register',views.register,name='register'),
    path('about',views.about,name='about'),
    path('service',views.Orderreg,name='service'),
    path('contact',views.contact,name='contact'),
]

你的Orderbook是一个表格,你不能保存表格,你需要保存它的数据

def Orderreg(request):
if request.method=='POST':
    fm=Orderbook(request.POST)
    if fm.is_valid():
       #your variable here which you pass
       variable = fm.cleaned_data['variable']

然后你需要用这个变量做一些逻辑,例如查询一个对象,然后你才能保存()这个对象

问题是您的 Orderbook 表单不是 ModelForm,它没有保存方法,所以只需将其替换为下面的代码

class Orderbook(forms.ModelForm):
    class Meta:
        model = Orders
        fields = '__all__'