Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 试图通过web浏览器将数据加载到数据库中:禁止保存()以防止由于未保存的相关对象而导致数据丢失';船舶';_Python_Html_Django - Fatal编程技术网

Python 试图通过web浏览器将数据加载到数据库中:禁止保存()以防止由于未保存的相关对象而导致数据丢失';船舶';

Python 试图通过web浏览器将数据加载到数据库中:禁止保存()以防止由于未保存的相关对象而导致数据丢失';船舶';,python,html,django,Python,Html,Django,我完全不知道为什么会出现这个错误。当我在pythonshell中运行类似的代码时,我从来没有遇到过这个错误,但是当我试图通过web浏览器执行此操作时,我遇到了这个错误。知道为什么会这样吗?仅供参考,我完全是初学者 型号: from django.contrib.auth.models import User class Hull (models.Model): hull = models.CharField(max_length = 33, ) def __str__(se

我完全不知道为什么会出现这个错误。当我在pythonshell中运行类似的代码时,我从来没有遇到过这个错误,但是当我试图通过web浏览器执行此操作时,我遇到了这个错误。知道为什么会这样吗?仅供参考,我完全是初学者

型号:

from django.contrib.auth.models import User


class Hull (models.Model):
    hull = models.CharField(max_length = 33, )

    def __str__(self):
        return self.hull

class Hull_Spec(models.Model):
    ship = models.ForeignKey(Hull, on_delete=models.CASCADE)
    speed = models.FloatField()
    depth = models.FloatField()
    remarks  = models.CharField(max_length =300)

    def __str__(self):
        return self.remarks
观点

html用户界面


<div>
    <form action = '/new_ship/' method = 'POST'> 
        {% csrf_token %}
            <table>
                <col>
                    <tr>
                        <td><input type = 'text' name = 'ship'>Hull</input></td>
                        <td><input type = 'text' name = 'speed'>Speed</input></td>
                    </tr>
                    <tr>    
                        <td><input type = 'text' name = 'depth'>Depth</input></td>
                        <td><input type = 'text' name = 'remarks'>Remarks</input></td>
                    </tr>
                    <tr>
                        <td><input type="submit" value="Submit Fields" id = 'button_1'></td>
                    </tr>
                </col>
            </table>
    </form>
</div>


{%csrf_令牌%}
船体
速度
深度
评论

您的
Hull
Z
)实例尚未保存到数据库(使用
.save()
)。这就是错误的原因

您需要首先创建
Hull
实例(实例通常较低(snake)大小写):

然后,您可以使用它创建
Hull\u Spec
实例:

hull_spec = Hull_spec.objects.create(
        hull=hull,
        speed = response.POST.get('speed'), 
        depth = response.POST.get('depth'), 
        remarks = response.POST.get('remarks')
    )
所以你的观点会变成:

def new_ship (response):
    hull = Hull.objects.create(hull = response.POST.get('ship'))
    hull_spec = Hull_spec.objects.create(
        hull=hull,
        speed = response.POST.get('speed'), 
        depth = response.POST.get('depth'), 
        remarks = response.POST.get('remarks')
    )

    return HttpResponse('Success')
另外请注意,将您的模型命名为
HullSpec
而不是
Hull\u Spec
,这是惯例

通常,将模型实例保存到db的两种方法是:

hull = Hull(hull="something")  # creates the instance
hull.save()    # saves it to the database

# or

hull = Hull.objects.create(hull="something")  # does it all in one go

您的
Hull
Z
)实例尚未保存到数据库(使用
.save()
)。这就是错误的原因

您需要首先创建
Hull
实例(实例通常较低(snake)大小写):

然后,您可以使用它创建
Hull\u Spec
实例:

hull_spec = Hull_spec.objects.create(
        hull=hull,
        speed = response.POST.get('speed'), 
        depth = response.POST.get('depth'), 
        remarks = response.POST.get('remarks')
    )
所以你的观点会变成:

def new_ship (response):
    hull = Hull.objects.create(hull = response.POST.get('ship'))
    hull_spec = Hull_spec.objects.create(
        hull=hull,
        speed = response.POST.get('speed'), 
        depth = response.POST.get('depth'), 
        remarks = response.POST.get('remarks')
    )

    return HttpResponse('Success')
另外请注意,将您的模型命名为
HullSpec
而不是
Hull\u Spec
,这是惯例

通常,将模型实例保存到db的两种方法是:

hull = Hull(hull="something")  # creates the instance
hull.save()    # saves it to the database

# or

hull = Hull.objects.create(hull="something")  # does it all in one go

您是否可以共享任何回溯以了解问题。您是否可以共享任何回溯以了解问题。