ValueError - Python Django





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















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:ProgramDataMiniconda3envsmyDjanEnvlibsite-packagesdjangodbmodelsfields__init__.py in get_prep_value, line 965
Python Executable: C:ProgramDataMiniconda3envsmyDjanEnvpython.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









share|improve this question

























  • Looks like you are passing the keyword create. You should share your views.py too.

    – Anton vBR
    Nov 24 '18 at 7:21











  • Post edited. I added view.py

    – sf31
    Nov 24 '18 at 7:28


















1















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:ProgramDataMiniconda3envsmyDjanEnvlibsite-packagesdjangodbmodelsfields__init__.py in get_prep_value, line 965
Python Executable: C:ProgramDataMiniconda3envsmyDjanEnvpython.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









share|improve this question

























  • Looks like you are passing the keyword create. You should share your views.py too.

    – Anton vBR
    Nov 24 '18 at 7:21











  • Post edited. I added view.py

    – sf31
    Nov 24 '18 at 7:28














1












1








1








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:ProgramDataMiniconda3envsmyDjanEnvlibsite-packagesdjangodbmodelsfields__init__.py in get_prep_value, line 965
Python Executable: C:ProgramDataMiniconda3envsmyDjanEnvpython.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









share|improve this question
















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:ProgramDataMiniconda3envsmyDjanEnvlibsite-packagesdjangodbmodelsfields__init__.py in get_prep_value, line 965
Python Executable: C:ProgramDataMiniconda3envsmyDjanEnvpython.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






python django valueerror






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 8:06









eyllanesc

88.2k103564




88.2k103564










asked Nov 24 '18 at 7:19









sf31sf31

204




204













  • Looks like you are passing the keyword create. You should share your views.py too.

    – Anton vBR
    Nov 24 '18 at 7:21











  • Post edited. I added view.py

    – sf31
    Nov 24 '18 at 7:28



















  • Looks like you are passing the keyword create. You should share your views.py too.

    – Anton vBR
    Nov 24 '18 at 7:21











  • Post edited. I added view.py

    – sf31
    Nov 24 '18 at 7:28

















Looks like you are passing the keyword create. You should share your views.py too.

– Anton vBR
Nov 24 '18 at 7:21





Looks like you are passing the keyword create. You should share your views.py too.

– Anton vBR
Nov 24 '18 at 7:21













Post edited. I added view.py

– sf31
Nov 24 '18 at 7:28





Post edited. I added view.py

– sf31
Nov 24 '18 at 7:28












1 Answer
1






active

oldest

votes


















3














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'),





share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456063%2fvalueerror-python-django%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    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'),





    share|improve this answer






























      3














      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'),





      share|improve this answer




























        3












        3








        3







        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'),





        share|improve this answer















        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'),






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 '18 at 7:57

























        answered Nov 24 '18 at 7:49









        Henry WoodyHenry Woody

        5,08531127




        5,08531127
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456063%2fvalueerror-python-django%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()