我通过论坛,对我的代码做了一些修改,但问题仍然存在。我确实进行了迁移和迁移,甚至删除并重新创建了它。
我还将default=0添加到models.py代码中:
年龄= 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):从django.db.models.expressions导入OuterRef值=超级().get_prep_value(值),如果值为None或isinstance( value,OuterRef):返回值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.nameurls.py文件在basic_app文件夹中:
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发布于 2018-11-24 07:49:05
问题是pk的regex捕获在urls.py中的详细路由中,特别是这一行:
url(r'^(?P<pk>[-\w]+)/$',views.schoolDetailView.as_view(),name='detail'),我认为您的意思是[-\w]是非alpha字符,但它搜索的是连字符(-)或任何alpha、数字或下划线字符(\w)。因此,当您转到路由/basic_app/create/时,'create'与模式[-\w]+匹配,并用作主键。由于'create'由alpha字符组成,不能转换为整数,因此将得到错误"invalid literal for int() with base 10: 'create'"。
要解决这个问题,我认为您只需要匹配细节路由的数字字符。你可以这样做:
url(r'^(?P<pk>\d+)/$',views.schoolDetailView.as_view(),name='detail'),https://stackoverflow.com/questions/53456063
复制相似问题