2016-04-23 7 views
0
、私は次のようなエラーになっています

(ベース10とint型のための無効なリテラル()「空自」)sqliteのジャンゴにデータを挿入する方法

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

私はジャンゴと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

オクラホマのような整数を試してみてください、ここでは2つの主要な問題 - 連絡先番号は整数の性質とは何の関係もないよう>

1. contact = models.IntegerField()これは、理想的には、整数として格納すべきではありません。 CharFieldを使ってみてください。あなたはそれが整数になりたい2.Ifは

、シンプル行う - ポイント2は、それを行う方法はありませんが>

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

を。連絡先番号が大きい場合は、intでサポートされていない可能性もあります。また、+ 21 232675 67612のようなものはプラス記号を持ち、intとして格納することはできません。