Python 数据库和模型仍然存在,DJango出现编程错误
在过去的3天里,我一直试图使用url.py和views.py在浏览器上获取模型数据,但失败惨重,尽管我能够在json文件中获取数据库,但无法在浏览器上获取。这是错误日志[代码在这里] 型号.pyPython 数据库和模型仍然存在,DJango出现编程错误,python,json,django,django-models,Python,Json,Django,Django Models,在过去的3天里,我一直试图使用url.py和views.py在浏览器上获取模型数据,但失败惨重,尽管我能够在json文件中获取数据库,但无法在浏览器上获取。这是错误日志[代码在这里] 型号.py from __future__ import unicode_literals from django.db import models class Authors(models.Model): aid = models.AutoField(primary_key=True) an
from __future__ import unicode_literals
from django.db import models
class Authors(models.Model):
aid = models.AutoField(primary_key=True)
aname = models.CharField(max_length=200, blank=True, null=True)
adescription = models.CharField(max_length=8000, blank=True, null=True)
class Books(models.Model):
bid = models.IntegerField(primary_key=True)
bname = models.CharField(max_length=200, blank=True, null=True)
bdescription = models.CharField(max_length=8000, blank=True, null=True)
class Bookauth(models.Model):
bid = models.ForeignKey(Books, models.DO_NOTHING, db_column='bid', blank=True, null=True)
aid = models.ForeignKey(Authors, models.DO_NOTHING, db_column='aid', blank=True, null=True)
from __future__ import unicode_literals
from django.shortcuts import render
import requests
from django.http import HttpResponse
from django.db import models
from .models import Books
import json
from django.core import serializers
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
def getObject(request):
all_books = Books.objects.all()
html = serializers.serialize('json', all_books)
return HttpResponse(html)
#data = serializers.serialize("json", Books.objects.all())
#return HttpResponse(data, mimetype='application/json')
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^non', views.index, name = 'index'),
url(r'^auth', views.author_search, name = 'AuthSearch'),
url(r'^book', views.getObject, name = 'Books')
]
视图.py
from __future__ import unicode_literals
from django.db import models
class Authors(models.Model):
aid = models.AutoField(primary_key=True)
aname = models.CharField(max_length=200, blank=True, null=True)
adescription = models.CharField(max_length=8000, blank=True, null=True)
class Books(models.Model):
bid = models.IntegerField(primary_key=True)
bname = models.CharField(max_length=200, blank=True, null=True)
bdescription = models.CharField(max_length=8000, blank=True, null=True)
class Bookauth(models.Model):
bid = models.ForeignKey(Books, models.DO_NOTHING, db_column='bid', blank=True, null=True)
aid = models.ForeignKey(Authors, models.DO_NOTHING, db_column='aid', blank=True, null=True)
from __future__ import unicode_literals
from django.shortcuts import render
import requests
from django.http import HttpResponse
from django.db import models
from .models import Books
import json
from django.core import serializers
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
def getObject(request):
all_books = Books.objects.all()
html = serializers.serialize('json', all_books)
return HttpResponse(html)
#data = serializers.serialize("json", Books.objects.all())
#return HttpResponse(data, mimetype='application/json')
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^non', views.index, name = 'index'),
url(r'^auth', views.author_search, name = 'AuthSearch'),
url(r'^book', views.getObject, name = 'Books')
]
url.py
from __future__ import unicode_literals
from django.db import models
class Authors(models.Model):
aid = models.AutoField(primary_key=True)
aname = models.CharField(max_length=200, blank=True, null=True)
adescription = models.CharField(max_length=8000, blank=True, null=True)
class Books(models.Model):
bid = models.IntegerField(primary_key=True)
bname = models.CharField(max_length=200, blank=True, null=True)
bdescription = models.CharField(max_length=8000, blank=True, null=True)
class Bookauth(models.Model):
bid = models.ForeignKey(Books, models.DO_NOTHING, db_column='bid', blank=True, null=True)
aid = models.ForeignKey(Authors, models.DO_NOTHING, db_column='aid', blank=True, null=True)
from __future__ import unicode_literals
from django.shortcuts import render
import requests
from django.http import HttpResponse
from django.db import models
from .models import Books
import json
from django.core import serializers
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
def getObject(request):
all_books = Books.objects.all()
html = serializers.serialize('json', all_books)
return HttpResponse(html)
#data = serializers.serialize("json", Books.objects.all())
#return HttpResponse(data, mimetype='application/json')
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^non', views.index, name = 'index'),
url(r'^auth', views.author_search, name = 'AuthSearch'),
url(r'^book', views.getObject, name = 'Books')
]
将模型更改为此,然后进行迁移,然后进行迁移,然后在浏览器中签入
from __future__ import unicode_literals
from django.db import models
class Authors(models.Model):
aid = models.AutoField(primary_key=True)
aname = models.CharField(max_length=200, blank=True, null=True)
adescription = models.TextField( blank=True, null=True)
class Books(models.Model):
bid = models.IntegerField(primary_key=True)
bname = models.CharField(max_length=200, blank=True, null=True)
bdescription = models.TextField(blank=True, null=True)
class Bookauth(models.Model):
bid = models.ForeignKey(Books, on_delete=models.DO_NOTHING, db_column='bid', blank=True, null=True)
aid = models.ForeignKey(Authors, on_delete=models.DO_NOTHING, db_column='aid', blank=True, null=True)
当您运行manage.py migrate时会发生什么情况?@DanielRoseman我的数据库也出现了错误,就像我在模型中更改了字段,但在MySql数据库中没有更改,但现在可以工作了!您能告诉我您更改了什么吗?您为CharField提供了8000个字符,但不正确,它可以容纳255个字符,因此我将其更改为TextField,其中可能包含大量字符,并且您在模型之前错过了_delete键。不执行任何操作