[英]ValueError - Python Django
I went through forums and did some changes in my codes but the problem is still there. 我通过论坛并对我的代码进行了一些更改,但问题仍然存在。 I did migrate and makemigrations, and even deleted migrate and recreate it again.
我做了迁移和makemigrations,甚至删除了迁移并再次重新创建它。
I also added default=0 to the models.py code: 我还在models.py代码中添加了default = 0:
age = models.PositiveIntegerField(default=0)
age = models.PositiveIntegerField(默认值= 0)
I'll appreciate you if you help me on this issue. 如果你在这个问题上帮助我,我将不胜感激。 These are some lines of codes, let me know if you need any other information.
这些是一些代码行,如果您需要任何其他信息,请告诉我。
This is the error: 这是错误:
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']
This is __init__.py file, line 960-965: 这是__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)
The models.py file: 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
urls.py file in basic_app folder: 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 file: 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
The issue is your regex capture for pk
in the detail route in urls.py, specifically this line: 问题是你在urls.py中详细路径中的
pk
的正则表达式捕获,特别是这一行:
url(r'^(?P<pk>[-\w]+)/$',views.schoolDetailView.as_view(),name='detail'),
I think you meant [-\\w]
as non alpha characters, but what this searches for is a hyphen ( -
) or any alpha, numeric, or underscore character ( \\w
). 我认为你的意思是
[-\\w]
为非字母字符,但搜索的是连字符( -
)或任何字母,数字或下划线字符( \\w
)。 Thus when you go to the route /basic_app/create/
, 'create'
matches the pattern [-\\w]+
and is used as the primary key. 因此,当您转到route
/basic_app/create/
, 'create'
匹配模式[-\\w]+
并用作主键。 Since 'create'
is composed of alpha characters and cannot be cast to an integer, you get the error "invalid literal for int() with base 10: 'create'"
. 由于
'create'
由字母字符组成且无法强制转换为整数,因此您会得到错误"invalid literal for int() with base 10: 'create'"
。
To fix this, I think you'll want to only match numeric characters for the detail route. 要解决这个问题,我想你只想匹配细节路线的数字字符。 You could do something like:
你可以这样做:
url(r'^(?P<pk>\d+)/$',views.schoolDetailView.as_view(),name='detail'),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.