Python ValueError:字段';长度';应为一个数字,但得到'';
Python ValueError:字段';长度';应为一个数字,但得到'';,python,django,Python,Django,型号 from django.db import models from django.contrib.auth.models import User from django.utils.timezone import now from django.contrib.auth.models import User # Create your models here. class Allmusic(models.Model): sno=models.AutoField(primary_
型号
from django.db import models
from django.contrib.auth.models import User
from django.utils.timezone import now
from django.contrib.auth.models import User
# Create your models here.
class Allmusic(models.Model):
sno=models.AutoField(primary_key=True)
name=models.CharField(max_length=100,default="")
author=models.CharField(max_length=100,default="")
description=models.TextField(default="")
movie=models.CharField(max_length=100,default="")
category=models.CharField(max_length=100,default="")
subcategory=models.CharField(max_length=100,default="")
image=models.ImageField(upload_to='images', default="")
musicfile=models.FileField(upload_to='music_file', default="")
def __str__(self):
return self.name
class Watchlater(models.Model):
watch_id=models.AutoField(primary_key=True)
user=models.ForeignKey(User,on_delete=models.CASCADE)
video_id=models.CharField(max_length=100000,default="")
class BlogComment(models.Model):
sno=models.AutoField(primary_key=True)
comment=models.TextField()
user=models.ForeignKey(User,on_delete=models.CASCADE)
post=models.ForeignKey(Allmusic,on_delete=models.CASCADE)
timestamp=models.DateTimeField(default=now)
class History(models.Model):
history_id=models.AutoField(primary_key=True)
user=models.ForeignKey(User,on_delete=models.CASCADE)
music_id=models.CharField(max_length=100000,default="")
views.py
from django.shortcuts import render,HttpResponse,redirect
from .models import Allmusic,Watchlater,BlogComment,History
from math import ceil
from django.contrib.auth.models import User
from django.contrib.auth import login,authenticate,logout
from django.contrib import messages
from django.db.models import Case,When
# Create your views here.
def home(request):
return render(request,'home.html')
def index(request):
allProds = []
catprods = Allmusic.objects.values('category', 'sno')
cats = {item['category'] for item in catprods}
for cat in cats:
prod = Allmusic.objects.filter(category=cat)
n = len(prod)
nSlides = n // 4 + ceil((n / 4) - (n // 4))
allProds.append([prod, range(1, nSlides), nSlides])
params = {'allProds':allProds}
return render(request,'index.html',params)
def indexmovie(request):
allProds = []
catprods = Allmusic.objects.values('movie', 'sno')
cats = {item['movie'] for item in catprods}
for cat in cats:
prod = Allmusic.objects.filter(movie=cat)
n = len(prod)
nSlides = n // 4 + ceil((n / 4) - (n // 4))
allProds.append([prod, range(1, nSlides), nSlides])
params = {'allProds':allProds}
return render(request,'indexmovie.html',params)
def about(request):
return render(request,'about.html')
def contact(request):
return render(request,'contact.html')
#Authenciation APIs
def handleSignup(request):
if request.method=='POST':
#Get the post parameters
username=request.POST['username']
fname=request.POST['fname']
lname=request.POST['lname']
email=request.POST['email']
pass1=request.POST['pass1']
pass2=request.POST['pass2']
#checks for errorneous input
#username should be <10
# username shouldbe alphanumeric
if len(username)>10:
messages.error(request,"username must be less than 10 characters")
return redirect('/')
if not username.isalnum():
messages.error(request,"username should only contain letters and numbers")
return redirect('/')
if pass1!=pass2:
messages.error(request,"Password do not match")
return redirect('/')
#Create the user
myuser=User.objects.create_user(username,email,pass1)
myuser.first_name=fname
myuser.last_name=lname
myuser.save()
messages.success(request,"your Musify account has been created succesfully Now go enter your credentials into the login form")
return redirect('/')
else:
return HttpResponse('404 - Not Found')
def handleLogin(request):
if request.method=='POST':
#Get the post parameters
loginusername=request.POST['loginusername']
loginpassword=request.POST['loginpassword']
user=authenticate(username=loginusername,password=loginpassword)
if user is not None:
login(request,user)
messages.success(request,"succesfully Logged In")
return redirect('/')
else:
messages.error(request,"Invalid credentials Please try again")
return redirect('/')
return HttpResponse('404 - Not Found')
def handleLogout(request):
logout(request)
messages.success(request,"succesully Logout")
return redirect('/')
def watchlater(request):
if request.method=="POST":
user=request.user
video_id=request.POST['video_id']
watch=Watchlater.objects.filter(user=user)
for i in watch:
if video_id==i.video_id:
messages.success(request,"Song is already added")
break
else:
wl=Watchlater(user=user,video_id=video_id)
wl.save()
messages.success(request,"Song added to watch later")
return redirect(f"/index/subpage/{video_id}")
wl=Watchlater.objects.filter(user=request.user)
ids=[]
for i in wl:
ids.append(i.video_id)
preserved=Case(*[When(pk=pk,then=pos) for pos,pk in enumerate(ids)])
song=Allmusic.objects.filter(sno__in=ids).order_by(preserved)
return render(request,'watchlater.html',{'song':song})
def subpage(request,sno):
post=Allmusic.objects.filter(sno=sno)
comment=BlogComment.objects.filter(post=sno)
context={'post':post,'comment':comment}
return render(request,'subpage.html',context)
def postComment(request):
comment=request.POST.get("comment")
user=request.user
postSno=request.POST.get("postSno")
post=Allmusic.objects.get(sno=postSno)
comment=BlogComment(comment=comment,user=user,post=post)
comment.save()
messages.success(request,"your comment has been posted")
return redirect(f"index/subpage/{post.sno}")
def history(request):
if request.method == "POST":
user=request.user
music_id=request.POST['music_id']
history=History(user=user,music_id=music_id)
history.save()
print(history)
return redirect(f"/index/subpage/{music_id}")
history=History.objects.filter(user=request.user)
ids=[]
for i in history:
ids.append(i.music_id)
preserved=Case(*[When(pk=pk,then=pos) for pos,pk in enumerate(ids)])
song=Allmusic.objects.filter(sno__in=ids).order_by(preserved)
return render(request,'history.html',{"history":song})
# Generated by Django 3.1 on 2020-10-03 12:49
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('ddsmusic', '0013_auto_20201002_2135'),
]
operations = [
migrations.AddField(
model_name='allmusic',
name='length',
field=models.IntegerField(default=2),
),
]
0014\u allmusic\u length.py
from django.shortcuts import render,HttpResponse,redirect
from .models import Allmusic,Watchlater,BlogComment,History
from math import ceil
from django.contrib.auth.models import User
from django.contrib.auth import login,authenticate,logout
from django.contrib import messages
from django.db.models import Case,When
# Create your views here.
def home(request):
return render(request,'home.html')
def index(request):
allProds = []
catprods = Allmusic.objects.values('category', 'sno')
cats = {item['category'] for item in catprods}
for cat in cats:
prod = Allmusic.objects.filter(category=cat)
n = len(prod)
nSlides = n // 4 + ceil((n / 4) - (n // 4))
allProds.append([prod, range(1, nSlides), nSlides])
params = {'allProds':allProds}
return render(request,'index.html',params)
def indexmovie(request):
allProds = []
catprods = Allmusic.objects.values('movie', 'sno')
cats = {item['movie'] for item in catprods}
for cat in cats:
prod = Allmusic.objects.filter(movie=cat)
n = len(prod)
nSlides = n // 4 + ceil((n / 4) - (n // 4))
allProds.append([prod, range(1, nSlides), nSlides])
params = {'allProds':allProds}
return render(request,'indexmovie.html',params)
def about(request):
return render(request,'about.html')
def contact(request):
return render(request,'contact.html')
#Authenciation APIs
def handleSignup(request):
if request.method=='POST':
#Get the post parameters
username=request.POST['username']
fname=request.POST['fname']
lname=request.POST['lname']
email=request.POST['email']
pass1=request.POST['pass1']
pass2=request.POST['pass2']
#checks for errorneous input
#username should be <10
# username shouldbe alphanumeric
if len(username)>10:
messages.error(request,"username must be less than 10 characters")
return redirect('/')
if not username.isalnum():
messages.error(request,"username should only contain letters and numbers")
return redirect('/')
if pass1!=pass2:
messages.error(request,"Password do not match")
return redirect('/')
#Create the user
myuser=User.objects.create_user(username,email,pass1)
myuser.first_name=fname
myuser.last_name=lname
myuser.save()
messages.success(request,"your Musify account has been created succesfully Now go enter your credentials into the login form")
return redirect('/')
else:
return HttpResponse('404 - Not Found')
def handleLogin(request):
if request.method=='POST':
#Get the post parameters
loginusername=request.POST['loginusername']
loginpassword=request.POST['loginpassword']
user=authenticate(username=loginusername,password=loginpassword)
if user is not None:
login(request,user)
messages.success(request,"succesfully Logged In")
return redirect('/')
else:
messages.error(request,"Invalid credentials Please try again")
return redirect('/')
return HttpResponse('404 - Not Found')
def handleLogout(request):
logout(request)
messages.success(request,"succesully Logout")
return redirect('/')
def watchlater(request):
if request.method=="POST":
user=request.user
video_id=request.POST['video_id']
watch=Watchlater.objects.filter(user=user)
for i in watch:
if video_id==i.video_id:
messages.success(request,"Song is already added")
break
else:
wl=Watchlater(user=user,video_id=video_id)
wl.save()
messages.success(request,"Song added to watch later")
return redirect(f"/index/subpage/{video_id}")
wl=Watchlater.objects.filter(user=request.user)
ids=[]
for i in wl:
ids.append(i.video_id)
preserved=Case(*[When(pk=pk,then=pos) for pos,pk in enumerate(ids)])
song=Allmusic.objects.filter(sno__in=ids).order_by(preserved)
return render(request,'watchlater.html',{'song':song})
def subpage(request,sno):
post=Allmusic.objects.filter(sno=sno)
comment=BlogComment.objects.filter(post=sno)
context={'post':post,'comment':comment}
return render(request,'subpage.html',context)
def postComment(request):
comment=request.POST.get("comment")
user=request.user
postSno=request.POST.get("postSno")
post=Allmusic.objects.get(sno=postSno)
comment=BlogComment(comment=comment,user=user,post=post)
comment.save()
messages.success(request,"your comment has been posted")
return redirect(f"index/subpage/{post.sno}")
def history(request):
if request.method == "POST":
user=request.user
music_id=request.POST['music_id']
history=History(user=user,music_id=music_id)
history.save()
print(history)
return redirect(f"/index/subpage/{music_id}")
history=History.objects.filter(user=request.user)
ids=[]
for i in history:
ids.append(i.music_id)
preserved=Case(*[When(pk=pk,then=pos) for pos,pk in enumerate(ids)])
song=Allmusic.objects.filter(sno__in=ids).order_by(preserved)
return render(request,'history.html',{"history":song})
# Generated by Django 3.1 on 2020-10-03 12:49
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('ddsmusic', '0013_auto_20201002_2135'),
]
operations = [
migrations.AddField(
model_name='allmusic',
name='length',
field=models.IntegerField(default=2),
),
]
ddsmusic.0014_allmusic_length
迁移文件是什么样子的?当我运行命令python manage.py migrateDhruvilShah时,会出现上述错误:但它源自您构建的迁移文件。因此,模型等都与此特定问题无关。以下是文件Willem Van Onsem“”#由Django 3.1于2020-10-03 12:49从Django.db导入迁移生成,模型类迁移(migrations.Migration):依赖项=[('ddsmusic','0013_auto_20201002_2135'),]操作=[migrations.AddField(model_name='allmusic',name='length',field=models.IntegerField(默认值=''),]“很明显,您添加了一个length
字段,其中default='
。您可能从模型中删除了该字段,但没有从迁移文件中删除,因此它将继续阻塞。您应该删除迁移文件(以及其他相关迁移文件)。