的AttributeError没有属性“对象”

时间:2019-05-08 10:30:14

标签: django django-models

AttributeError类型的对象'Data_Point'没有属性'objects',请检查并纠正我

  

AttributeError位于/   类型对象“ myProduction”没有属性“对象”

模型”:

from django.db import models
from django.contrib.auth.models import User
class Production(models.Model):
   title=models.CharField(max_length=120)
   def __str__(self):
      return self.title

我的表单

from  django import forms
from.models import Production
class myProduction(forms.ModelForm):
    class Meta:
        model=Production
        fields =['title'] 

class Raw_Pro(forms.Form):
    title = forms.CharField()

我的视图

from django.shortcuts import render
from .form import myProduction,Raw_Pro
def my_index(request):
    my_form=Raw_Pro()
    if request.method=='POST':
        my_form=Raw_Pro(request.POST)
        if my_form.is_valid():
            myProduction.objects.create(my_form.cleaned_data)
        else:
             print(my_form.errors)
    context={"form":my_form}
    return render(request, "index.html",context)

1 个答案:

答案 0 :(得分:2)

您在这里犯了一些错误:

  1. myProduction是您的ModelForm(在forms.py中定义),而不是您的模型(这是Production,在`models.py中定义);
  2. 您在这里使用Raw_Pro作为表单,而不是ModelForm,这可能不是您想使用的形式;
  3. 如果表格成功,则可以使用mymodelform.save()创建/编辑对象;和
  4. 如果创建成功,则应重定向到页面,例如同一页面。否则,刷新用户将触发具有相同参数的POST。
from django.shortcuts import render
from .form import myProduction

def my_index(request):
    if request.method == 'POST':
        my_form = myProduction(request.POST)
        if my_form.is_valid():
            my_form.save()
            return redirect(my_index)  # or somewhere else
    else:
        my_form = myProduction()
    context = {"form":my_form}
    return render(request, "index.html",context)
  

注意:按照PEP-8 [Python-doc]的规定,您应使用以 U 大小写开头的驼峰式大写字母作为类名。因此,最好将myProduction类重命名为MyProduction,或者将ProductionForm重命名为<!DOCTYPE html> <html> <head> <title>Desk+ - Grupo 36</title> <link rel="stylesheet" type="text/css" href="PF1.1.css"> </head> <body> <div id="Logout"> <!---<a href="logout.php" id="LogoutAction"><img src="https://cdn2.iconfinder.com/data/icons/crystalproject/crystal_project_256x256/apps/exit.png"></a>--> <button onclick="location.href = 'PF1.html';" id="BtnTesteLogout" class="float-left submit-button" ><img src="https://cdn2.iconfinder.com/data/icons/crystalproject/crystal_project_256x256/apps/exit.png"></button> </div> <div id="BarraSDireita"> <div id="Bluetooth"> <nav id="wrapperB" class="wrapper"> <ul id="mainB" class="main"> <li id="frontB" class="front"><a href="#" class="botao"><img id ="BlueButton" src="https://cdn.pixabay.com/photo/2015/03/12/12/22/bluetooth-670069_960_720.png"></a> <ul id="subB" class="sub"> <li class="bottom" id="onoff"><a> <label class="switch"><input type="checkbox" id="togBtn"><div class="slider round"><!--ADDED HTML --><span class="on">ON</span><span class="off">OFF</span><!--END--></div></label></a></li> <li class="bottom" id="dispos"><a>Dispositivos emparelhados:</a></li> <li class="bottom" id="jphone"><a onclick="abrirBlue()">Joana's Phone</a></li> <li class="bottom" id="mphone"><a>Maria's Phone</a></li> </ul> </li> </ul> </nav> </div> <div id="clockdate"> <div class="clockdate-wrapper"> <div id="clock"></div> <div id="date"></div> </div> </div> <div id="SplitScreen"> <nav id="wrapperSS" class="wrapper"> <ul id="mainSS" class="main"> <li id="frontSS" class="front"><a href="#" class="botao"><img id ="SSButton" src="bullet-splitscreen.png"></a> <ul id="subSS" class="sub"> <li class="bottom" id="mergeall"><a href="#">Juntar todos os ecr&atildes</a></li> <li class="bottom" id="split2"><a href="#">Dividir em 2</a></li> <li class="bottom" id="split4"><a href="#">Dividir em 4</a></li> <li class="bottom" id="split6"><a href="#">Dividir em 6</a></li> </ul> </li> </ul> </nav> </div> <div id="WIFI"> <button id="WIFIButton" class="botao"><img src="https://purepng.com/public/uploads/large/purepng.com-wifi-icon-redwifi-iconwifiiconwireless-connection-1701528436213ktjmp.png"></button> </div> </div> <div id="BlueWindow" style="display:none;"> <div id="BlueWindowheader"> <header class="windowTop1"> <button id="closeButtonBlue"><img id="closeBlue" class="X" src="http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/256/Actions-dialog-close-icon.png" alt="X" onclick="fecharBlue()"></button> <h1 id="JoanaPTexto">Joana's Phone</h1> </header> </div> <div id="bottomBlue"> <div id="doc1"> <div id="doc1header"> <img id="DocImgBlue" src="https://img.icons8.com/pastel-glyph/2x/file.png"> </div> <h1 id="MariaDoc">Doc-exemplo-Joana</h1> </div> </div> </div> <div id="DOCS" class="botao" > <img id="documentos_logo"src="https://marketplace.canva.com/MAB7OE9Jppw/1/thumbnail_large/canva-folder-file-yellow-document-info-icon.-vector-graphic-MAB7OE9Jppw.png" alt="documentos" onclick="openDocs()"> </div> <div id="janela"> </div> <div id="docwindow" style="display: none;"> <div id="docwindowheader"> <header class="windowTop"> <h1 id="docsHeadTexto">Documentos</h1> <img id="closeDocs" class="X" src="http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/256/Actions-dialog-close-icon.png" alt="X" onclick="closeMain()"> </header> </div> <div id="bottomWindowDocs"> <div class="documents"> <div> <img id="DocImg" src="https://img.icons8.com/pastel-glyph/2x/file.png" alt="doc"> <h1 id="DocEx">Doc-exemplo</h1> </div> </div> <!----<div id="DocExemplo" S> <header class="windowhead"> Documento exemplo <img class="X" src="https://banner2.kisspng.com/20180402/dwe/kisspng-computer-icons-social-media-email-webmail-cancel-button-5ac240963875f3.0504665115226799582313.jpg" alt="X" onclick="closeMain()"> <button id="share">share</button> <button id="back">back</button> </header> <div id="corpo"> <h4>Este é um exemplo de Documento</h4> </div> </div>--> </div> </div> <div id="calendario"> <a id="iconeCal" class="botao" onclick="abrirCal()"><img src="https://www.manisteelibrary.org/images/events-calendar.png/@@images/image.png"></a> </div> <div id="janelaCalendario" style="display:none;"> <div id="janelaCalendarioHeader"> <header class="windowTop"> <img id="closeCal" class="X" src="http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/256/Actions-dialog-close-icon.png" alt="X" onclick="fecharCal()"> </header> </div> <iframe src="https://teamup.com/ksbe6drnzpwdbyrgp2" id="calendar"></iframe> </div> <div id="net" style=display:none;> <div id="Netheader"> <header class="windowTop2"> <h1 id="NetTexto">Desk+ Browser</h1> <img id="closeNet" class="X" src="http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/256/Actions-dialog-close-icon.png" alt="X" onclick="fecharNet()"> </header> </div> <div id="netBottom1"> <iframe id="netBottom" src="https://www.bing.com/" id="google"></iframe> </div> </div> <div class="navbar"> <a id="NET" onclick="openNet()" class="botao"><img src="NET.png" id="NetImg"></a> <a onclick="openAdressb()" class="botao" ><img src="http://download.seaicons.com/icons/paomedia/small-n-flat/1024/address-book-alt-icon.png" id="contact"></a> </div> </body> <script src="PF1.1.js"></script> </html> ,因为这样就很清楚该类在做什么。