值错误 - Python Django
Posted
技术标签:
【中文标题】值错误 - Python Django【英文标题】:ValueError - Python Django 【发布时间】:2019-04-26 14:52:09 【问题描述】:我浏览了论坛并对我的代码进行了一些更改,但问题仍然存在。我确实 migrate 和 makemigrations,甚至删除了 migrate 并重新创建它。
我还在 models.py 代码中添加了 default=0:
age = models.PositiveIntegerField(default=0)
如果您在这个问题上帮助我,我将不胜感激。 这些是一些代码行,如果您需要任何其他信息,请告诉我。
这是错误:
ValueError at /basic_app/create/
invalid literal for int() with base 10: 'create'
Request Method: GET
Request URL: http://127.0.0.1:8000/basic_app/create/
Django Version: 2.1.2
Exception Type: ValueError
Exception Value:
invalid literal for int() with base 10: 'create'
Exception Location: C:\ProgramData\Miniconda3\envs\myDjanEnv\lib\site-packages\django\db\models\fields\__init__.py in get_prep_value, line 965
Python Executable: C:\ProgramData\Miniconda3\envs\myDjanEnv\python.exe
Python Version: 3.7.0
Python Path:
['C:\\Users\\Administrator\\Desktop\\django_lecture\\djan_advance\\advcbv',
'C:\\ProgramData\\Miniconda3\\envs\\myDjanEnv\\python37.zip',
'C:\\ProgramData\\Miniconda3\\envs\\myDjanEnv\\DLLs',
'C:\\ProgramData\\Miniconda3\\envs\\myDjanEnv\\lib',
'C:\\ProgramData\\Miniconda3\\envs\\myDjanEnv',
'C:\\ProgramData\\Miniconda3\\envs\\myDjanEnv\\lib\\site-packages']
这是 __init__.py 文件,第 960-965 行:
def get_prep_value(self, value): from django.db.models.expressions import OuterRef value = super().get_prep_value(value) if value is None or isinstance(value, OuterRef): return value return int(value)
models.py 文件:
from django.db import models
# Create your models here.
class School(models.Model):
name = models.CharField(max_length=128)
principal = models.CharField(max_length=128)
location = models.CharField(max_length=128)
def __str__(self):
return self.name
class Student(models.Model):
name = models.CharField(max_length=128)
age = models.PositiveIntegerField()
school = models.ForeignKey(School,related_name='students',on_delete=models.PROTECT)
def __str__(self):
return self.name
basic_app 文件夹中的 urls.py 文件:
from django.conf.urls import url
from basic_app import views
app_name = 'basic_app'
urlpatterns = [
url(r'^$',views.schoolListView.as_view(),name='list'),
url(r'^(?P<pk>[-\w]+)/$',views.schoolDetailView.as_view(),name='detail'),
url(r'^create/$',views.schoolCreateView.as_view(),name='create')
]
views.py 文件:
from django.shortcuts import render
from django.views.generic import (View,TemplateView,
ListView,DetailView,
CreateView,UpdateView,
DeleteView)
from . import models
class IndexView(TemplateView):
template_name = 'index.html'
class schoolListView(ListView):
context_object_name = 'schools'
model = models.School
class schoolDetailView(DetailView):
context_object_name = 'school_detail'
model = models.School
template_name = 'basic_app/school_detail.html'
class schoolCreateView(CreateView):
fields = ('name','principal','location')
model = models.School
【问题讨论】:
看起来您正在传递关键字 create。你也应该分享你的views.py。 帖子已编辑。我添加了 view.py 【参考方案1】:问题是您在 urls.py 的详细路由中对 pk
的正则表达式捕获,特别是这一行:
url(r'^(?P<pk>[-\w]+)/$',views.schoolDetailView.as_view(),name='detail'),
我认为您的意思是 [-\w]
是非字母字符,但它搜索的是连字符 (-
) 或任何字母、数字或下划线字符 (\w
)。因此,当您转到路线/basic_app/create/
时,'create'
匹配模式[-\w]+
并用作主键。由于'create'
由字母字符组成,不能转换为整数,因此您会收到错误"invalid literal for int() with base 10: 'create'"
。
要解决此问题,我认为您只想匹配详细路线的数字字符。你可以这样做:
url(r'^(?P<pk>\d+)/$',views.schoolDetailView.as_view(),name='detail'),
【讨论】:
以上是关于值错误 - Python Django的主要内容,如果未能解决你的问题,请参考以下文章
如何修复以下 Django 错误:“类型:IOError”“值:[Errno 13] 权限被拒绝”