异常类型:ValueError at /music/1/favourite/ 异常值:int() 的无效文字,基数为 10:'on'
Posted
技术标签:
【中文标题】异常类型:ValueError at /music/1/favourite/ 异常值:int() 的无效文字,基数为 10:\'on\'【英文标题】:Exception Type: ValueError at /music/1/favourite/ Exception Value: invalid literal for int() with base 10: 'on'异常类型:ValueError at /music/1/favourite/ 异常值:int() 的无效文字,基数为 10:'on' 【发布时间】:2019-01-01 01:05:28 【问题描述】:我是 Django 新手,我正在使用它的最新版本 2。我指的是这个特定的教程 https://www.youtube.com/watch?v=irH98-4eKmQ&list=PL6gx4Cwl9DGBlmzzFcLgDhKTTfNLfX1IK&index=24
我收到此错误。我尝试了很多,但无法解决问题。 /music/1/favourite/处的值错误
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/music/1/favourite/
Django Version: 2.0.7
Python Version: 3.6.6
Installed Applications:
['music.apps.MusicConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e,
request)
File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args,
**callback_kwargs)
File "C:\Users\Adesh\Desktop\website\music\views.py" in favourite
22. selected_song = album.song_set.get(pk=request.POST['song'])
File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\db\models\manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args,
**kwargs)
File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\db\models\query.py" in get
394. clone = self.filter(*args, **kwargs)
File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\db\models\query.py" in filter
836. return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\db\models\query.py" in _filter_or_exclude
854. clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\db\models\sql\query.py" in add_q
1253. clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\db\models\sql\query.py" in _add_q
1277. split_subq=split_subq,
File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\db\models\sql\query.py" in build_filter
1215. condition = self.build_lookup(lookups, col, value)
File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\db\models\sql\query.py" in build_lookup
1085. lookup = lookup_class(lhs, rhs)
File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\db\models\lookups.py" in __init__
18. self.rhs = self.get_prep_lookup()
File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\db\models\lookups.py" in get_prep_lookup
68. return self.lhs.output_field.get_prep_value(self.rhs)
File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\db\models\fields\__init__.py" in get_prep_value
947. return int(value)
Exception Type: ValueError at /music/1/favourite/
Exception Value: invalid literal for int() with base 10: 'on'
这是我的 details.html(模板)
<img src=" album.album_logo">
<h1> album.album_title </h1>
<h2> album.artist </h2>
% if error_message %
<p><strong> error_message </strong></p>
% endif %
<form action="% url 'music:favourite' album.id %" method="post" >
% csrf_token %
% for song in album.song_set.all %
<input type="radio" id="song forloop.counter " name="song" value" song.id "
<label for="song forloop.counter ">
song.song_title
% if song.is_favourite %
<img src="random_img.png"/>
% endif %
</label><br>
% endfor %
<input tye="submit" value="Favourite">
</form>
这是我的观点.py
from django.shortcuts import render, get_object_or_404
from .models import Album, Song
def index(request):
all_albums = Album.objects.all()
context=
'all_albums':all_albums,
return render(request, 'music/index.html',context)
def detail(request,album_id):
album = get_object_or_404(Album, pk=album_id)
return render(request, 'music/detail.html','album':album)
def favourite(request, album_id):
album = get_object_or_404(Album, pk=album_id)
try:
selected_song = album.song_set.get(pk=request.POST['song'])
except (KeyError, Song.DoesNotExist):
return render(request, 'music/details.html',
'album': album,
'error_message':"You did not select a valid song",
)
else:
selected_song.is_favourite = True
selected_song.save()
return render(request, 'music/detail.html','album': album)
【问题讨论】:
错误与album.song_set.get(pk=request.POST['song'])
有关,'song'
不包含id,但'on'
。
【参考方案1】:
您的模板中的以下行存在一些问题:
<input type="radio" id="song forloop.counter " name="song" value" song.id "
两个重要的问题是:
-
您没有关闭标签(使用
>
);和
您忘记了value
属性的=
:它应该是value=" song.id "
而不是value" song.id "
。
所以当我们修复这些问题时,我们得到:
<input type="radio" id="song forloop.counter " name="song" value=" song.id ">
由于您在value
属性上出现错误,根据浏览器的说法,没有value="..."
属性,因此它对'on'
做了一个“* fallback*”作为默认字符串可以这么说.
【讨论】:
【参考方案2】:在 django shell 中输入 ./manage.py shell
试试这个
from .models import Album, Song
[song.id for song in Song.objects.all()]
# This will display all the songs id's of your Song model.
现在检查所有歌曲对象是否只有整数 id 并分享结果
【讨论】:
以上是关于异常类型:ValueError at /music/1/favourite/ 异常值:int() 的无效文字,基数为 10:'on'的主要内容,如果未能解决你的问题,请参考以下文章
ValueError: expected sequence of length 3573 at dim 0 (got 768)
TemplateDoesNotExist at / Home.html 异常类型:TemplateDoesNotExist
ValueError: need at most 63 handles, got a sequence of length 65