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

时间:2017-06-24 06:52:20

标签: python django

使用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.

1 个答案:

答案 0 :(得分:1)

检查您的应用bookingservice是否在installed_apps的{​​{1}}内。如果没有,请在installed_apps中添加您的应用settings.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