异常类型:/music/1/favourite 处的值错误/异常值:以 10 为底的 int() 的无效文字:'on'
Exception Type: ValueError at /music/1/favourite/ Exception Value: invalid literal for int() with base 10: 'on'
我是 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>
这是我的view.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})
您的模板中的以下行存在一些问题:
<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="..."
属性,因此它对 [=19= 执行了“*回退*” ] 作为默认字符串可以这么说。
在 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 并分享结果
我是 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>
这是我的view.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})
您的模板中的以下行存在一些问题:
<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="..."
属性,因此它对 [=19= 执行了“*回退*” ] 作为默认字符串可以这么说。
在 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 并分享结果