使用Python和Djang将数据插入数据库时出错

2024-04-25 01:29:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用Python和Django将数据插入数据库时遇到以下错误。你知道吗

OperationalError at /insert/
no such table: bookingservice_service
Request Method: POST
Request URL:    http://127.0.0.1:8000/insert/
Django Version: 1.11.2
Exception Type: OperationalError
Exception Value:    
no such table: bookingservice_service
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py in execute, line 328
Python Executable:  /usr/bin/python
Python Version: 2.7.6
Python Path:    
['/opt/lampp/htdocs/carClinic',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

这是我的密码:

预订服务/型号.py地址:

from __future__ import unicode_literals

from django.db import models
from django.contrib import admin
from datetime import datetime

class Service(models.Model):
    """In this class the columns for service table has declared"""
    cname = models.CharField(max_length=200)
    date_of_service = models.DateTimeField(default=datetime.now, blank=True)
    vechile_no = models.CharField(max_length=200)
    service_type = models.CharField(max_length=200)

class Personal(models.Model):
    """In this class the columns for Person table has declared"""
    name = models.CharField(max_length=200)
    address = models.CharField(max_length=200)
    phone = models.CharField(max_length=15)
    driving_license = models.CharField(max_length=200)
    email = models.CharField(max_length=200)
    date = models.DateTimeField(default=datetime.now, blank=True)

admin.site.register(Personal)
admin.site.register(Service)

我的views.py文件如下。你知道吗

from __future__ import unicode_literals

from django.shortcuts import render, redirect
from django.contrib.auth import login
from django.views.generic import View
from django.contrib.auth.forms import (UserCreationForm, AuthenticationForm)
from bookingservice.models import Service, Personal

def booking(request):
    return render(request, 'bookingservice/booking.html', {})

def insert(request):
    if request.method == 'POST':
        company_name = request.POST.get('cname')
        vechicle_no = request.POST.get('vechileno')
        service_type = request.POST.get('sertype')
        s = Service( 
        cname=company_name, 
        vechile_no=vechicle_no, 
        service_type=service_type
        ) 
        s.save()
    return render(request, 'bookingservice/booking.html', {})

def home(request):
    return render(request, 'bookingservice/home.html', {})

我已经迁移了模型,它显示了下面的消息。你知道吗

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.
  Your models have changes that are not yet reflected in a migration, and so won't be applied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

Tags: djangonofromimportmodelsrequestlibpackages
1条回答
网友
1楼 · 发布于 2024-04-25 01:29:21

检查你的应用程序bookingservice是否在installed_appssettings.py。如果没有,则在已安装的应用程序中添加应用程序bookingservice。你知道吗

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    ...
    'bookingservice',
]

然后再次执行迁移

manage.py makemigrations

然后呢

manage.py migrate

如果你的应用程序已经在installed_apps中,那么执行

manage.py makemigrations bookingservice

以及

manage.py migrate bookingservice

相关问题 更多 >