gpt4 book ai didi

python - Django 以 10 为基数的 int() 的文字无效 get_prep_value

转载 作者:行者123 更新时间:2023-12-01 04:31:52 24 4
gpt4 key购买 nike

我已经没有办法解决 int() 无效文字的问题了。

它最初有效,但我改变了一些东西,现在我遇到了这个问题。我从头开始设置项目并再次从头开始设置数据库,但它也没有帮助。

这是我的代码:

models.py

from django.db import models

# Create your models here.
class Zone(models.Model):
zone_name = models.CharField(max_length = 10)
zone_number = models.CharField(max_length = 10)

class Meta:
ordering = ('zone_name',)

def __unicode__(self):
return self.zone_name


class Stage(models.Model):
stage_number = models.CharField(max_length = 10)
stage_name = models.CharField(max_length = 10)
zones = models.ManyToManyField(Zone, through='ZoneStage')

class Meta:
ordering = ('stage_number',)

def __unicode__(self):
return self.stage_number


class ZoneStage(models.Model):
zone = models.ForeignKey(Zone)
stage = models.ForeignKey(Stage)
value = models.PositiveSmallIntegerField(default=0)

class Meta:
ordering = ('zone',)

def __unicode__(self):
return '%s %s' % (self.zone, self.stage)

views.py

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic

from models import Zone, Stage, ZoneStage
#from models import SubStage, ZoneSubStage

class IndexView(generic.ListView):
template_name = 'autostages/index.html'
context_object_name = 'stages'
queryset = Stage.objects.order_by('stage_number')[:12]

def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['zones'] = Zone.objects.all()
context['zone_stages'] = ZoneStage.objects.all()
# context['substages'] = SubStage.objects.all()

# And so on for more models
return context


class DetailView(generic.DetailView):
template_name = 'autostages/detail.html'
context_object_name = 'stages'
queryset = Stage.objects.all()

def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
context['zones'] = Zone.objects.all()
# context['zone_substages'] = ZoneSubStage.objects.all()
# context['substages'] = SubStage.objects.all()

# And so on for more models
return context

urls.py

from django.conf.urls import url

from . import views


urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='stage_list'),
url(r'^(?P<pk>\w+)/$', views.DetailView.as_view(), name='detail'),
]

index.html

<table border="1">
<tr>
<td style="width:40px" >
<td style="width:100px">
{% for zone in zones|dictsort:"zone_name" %}
<td style="width:40px" align="center">{{ zone.zone_name }}</td>
{% endfor %}
</tr>
{% for stage in stages %}
<tr>
<td style="width:40px"><a href="{% url 'autostages:detail' stage %}">{{ stage.stage_number }}
</a></td>
<td style="width:40px">{{ stage.stage_name }}</td>

{% for zone in zones|dictsort:"zone_name" %}
<td style="width:40px" align="center">
{% for zs in stage.zonestage_set.all %}
{% if zs.zone == zone %} {{ stage.value}}{% endif %}
<!--{% if zs.zone == zone %} {{ zs.value}}{% endif %}-->
{% endfor %}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>

detail.html

<table border="1">
<tr>
<td style="width:40px"></td>
<td style="width:100px"></td>

{% for zone in zones|dictsort:"zone_name" %}
{{ zone }}
<td style="width:40px" align="center">{{ zone.zone_name }}</td>
{% endfor %}

</tr>
<td style="width:40px">{{ stages }}</td>
<td style="width:100px">{{ stages.stage_name }}</td>
{% for substage in stages.substage_set.all %}
<tr>
<td style="width:40px"></td>
<td style="width:40px">{{ substage.sub_name }}</td>
{% for zone in zones|dictsort:"zone_name" %}
<td style="width:40px" align="center">
{% for zss in substage.zonesubstage_set.all %}
{% if zss.zone == zone %} {{ zss.value}}{% endif %}
{% endfor %}
</td>
{% endfor %}

</tr>

{% endfor %}
</table>

Django 回溯:

Environment:


Request Method: GET
Request URL: http://localhost:8000/autostages/S1/

Django Version: 1.8.4
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'autostages')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')


Traceback:
File "/home/dkr103/Coding/projects/env1/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/dkr103/Coding/projects/env1/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "/home/dkr103/Coding/projects/env1/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "/home/dkr103/Coding/projects/env1/local/lib/python2.7/site-packages/django/views/generic/detail.py" in get
115. self.object = self.get_object()
File "/home/dkr103/Coding/projects/env1/local/lib/python2.7/site-packages/django/views/generic/detail.py" in get_object
38. queryset = queryset.filter(pk=pk)
File "/home/dkr103/Coding/projects/env1/local/lib/python2.7/site-packages/django/db/models/query.py" in filter
679. return self._filter_or_exclude(False, *args, **kwargs)
File "/home/dkr103/Coding/projects/env1/local/lib/python2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
697. clone.query.add_q(Q(*args, **kwargs))
File "/home/dkr103/Coding/projects/env1/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_q
1309. clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/home/dkr103/Coding/projects/env1/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in _add_q
1337. allow_joins=allow_joins, split_subq=split_subq,
File "/home/dkr103/Coding/projects/env1/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in build_filter
1208. condition = self.build_lookup(lookups, col, value)
File "/home/dkr103/Coding/projects/env1/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in build_lookup
1101. return final_lookup(lhs, rhs)
File "/home/dkr103/Coding/projects/env1/local/lib/python2.7/site-packages/django/db/models/lookups.py" in __init__
101. self.rhs = self.get_prep_lookup()
File "/home/dkr103/Coding/projects/env1/local/lib/python2.7/site-packages/django/db/models/lookups.py" in get_prep_lookup
139. return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "/home/dkr103/Coding/projects/env1/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
727. return self.get_prep_value(value)
File "/home/dkr103/Coding/projects/env1/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
985. return int(value)

Exception Type: ValueError at /autostages/S1/
Exception Value: invalid literal for int() with base 10: 'S1'

最佳答案

我认为您错误地传递了 stage 对象,而不是 stage.pk 作为 index 中 url 模板标记中的参数.html 页面。

尝试改变

{% url 'autostages:detail' stage %}

{% url 'autostages:detail' stage.pk %}

实际上,当您传递 stage 对象作为参数时,您在模型中定义的表示形式,即 stage_number 被用作参数的值。 S1 不是整数导致错误。

关于python - Django 以 10 为基数的 int() 的文字无效 get_prep_value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32285769/

24 4 0
文章推荐: python - 从 sqlite3 转储数据时出错
文章推荐: jquery - 将 JSON 对象传递给操作并映射到 List