Reputation: 45
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.
I also added default=0 to the models.py code:
age = models.PositiveIntegerField(default=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:
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:
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:
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:
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
Upvotes: 2
Views: 2590
Reputation: 15652
The issue is your regex capture for pk
in the detail route in urls.py, specifically this line:
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
). Thus when you go to the route /basic_app/create/
, 'create'
matches the pattern [-\w]+
and is used as the primary key. 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'"
.
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'),
Upvotes: 5