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

问题描述 投票:-1回答:1

使用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']

我在下面解释我的代码。

bookingservice / models.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.
python django
1个回答
1
投票

检查你的应用程序bookingservice是否在installed_appssettings.py内。如果没有,那么在installed_apps中添加你的应用程序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
© www.soinside.com 2019 - 2024. All rights reserved.