ValueError - Python Django

ValueError - Python Django

我浏览了论坛并对我的代码做了一些更改,但问题仍然存在。我做了 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

urls.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

问题是您在 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'),