2016-04-23 52 views
0

我收到以下錯誤:如何(基數爲10無效字面INT():「ASDF」)插入sqlite的Django的數據

(invalid literal for int() with base 10: 'any thing'

我絕對新在Django和Python,我想通過Django的ORM插入數據表,但我不能和我一起努力解決它,但不同的錯誤來自不同的階段,請幫我解釋一下:

這是我的views.py

from django.shortcuts import render 
from django.http import HttpResponse, request, Http404, HttpResponseRedirect 
from django.template import Context 
from django.template.loader import get_template 
from django.contrib.auth.models import User 
from django.contrib.auth import logout 
from .models import Signup 
from django.shortcuts import render_to_response 
import sqlite3 


def first_page(request): 
    username = request.GET['txtname'] 
    password = request.GET['txtpassword'] 
    contact = request.GET['txtcontact'] 
    address = request.GET['txtaddress'] 

    # db = sqlite3.connect('mydatabase') 
    # cursor = db.cursor() 
    # cursor.execute('''INSERT INTO Signup(name, password, contact, address) VALUES (?,?,?,?)''', (username, password, contact, address)) 
    signup = Signup(username, password, contact, address) 
    signup.save() 
    return render_to_response("first_page.html", {'name': username, 
                'password': password, 
                'contact': contact, 
                'address': address}) 


def sign_up(request): 
    return render_to_response("signup.html") 

這裏是我的models.py

from django.db import models 


class Signup(models.Model): 
    name = models.CharField(max_length=200) 
    password = models.CharField(max_length=50) 
    contact = models.IntegerField() 
    address = models.CharField(max_length=250) 

這裏是我的sigup.html

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Sign Up Form</title> 
</head> 
<body> 
    <form action="." method="get"> 

     <label for="txtname">Name</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <input type="text" id="txtname" name="txtname"/><br/><br/><br/> 

     <label for="txtpassword">Password</label>&nbsp;&nbsp; 
     <input type="password" id="txtpassword" name="txtpassword"/><br/><br/><br/> 

     <label for="txtcontact">Contact</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <input type="number" id="txtcontact" name="txtcontact"/><br/><br/><br/> 

     <label for="txtaddress">Address</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <input type="text" id="txtaddress" name="txtaddress"/><br/><br/><br/> 

     <input type="submit" id="btnsignup" name="btnsignup" value="Register"> 

    </form> 

</body> 
</html> 

這裏是我的urls.py

from django.conf.urls import url 
from django.contrib import admin 
from reg.views import * 

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^first/', first_page), 
    url(r'^register$', sign_up), 
    url(r'^$', first_page), 
] 
+1

請發佈錯誤的堆棧跟蹤。 – solarissmoke

+0

另外,哪些操作和值導致錯誤?只用錯誤消息和大量代碼很難。 –

回答

0

基於你已經在這裏提到的錯誤invalid literal for int() with base 10: 'any thing'由於您將聯繫人定義爲IntegerField(),然後您通過了其value作爲任何東西在您的聯繫人文本框字段中。嘗試像123

0

好了,兩個主要問題整數位置 - >

1. contact = models.IntegerField()這應該不理想存儲爲整數,作爲聯繫號碼無關與整數性能。嘗試使用CharField。

2.如果您希望它是整數,做一個簡單的 - >

contact = request.GET['txtcontact'] 
contactInt = int(request.GET['txtcontact']) 
signup = Signup(username, password, contactInt, address) 

雖然第2點是不這樣做的方式。如果聯繫人數量很大,甚至可能得不到int的支持。另外,像+21 232675 67612這樣的東西有一個加號,不能存儲爲int。

相關問題