Django 中的视图错误
我以这种方式使用了 Django 的形式,但出现了错误: 错误:以 10 为基数的 int() 的文字无效:'check
'
#this is forms.py
from django import forms
class PersonalInfo(forms.Form):
Name = forms.CharField(max_length=20)
Email_ID = forms.EmailField(required=False)
Address = forms.CharField(max_length=50,required=False)
Contact_Phone = forms.CharField(max_length=20)
Image = forms.FileField(required=False)
PersonalInfo 在 register.html 中使用
#This is view.py, register calling register.html
def register(request):
form = PersonalInfo()
return render_to_response('register.html', {'form':form}, context_instance=RequestContext(request))
在 register.html 中,这就是我使用它的方式:
{% if form.errors %}
<p style="color: red;">
Please correct the error{{ form.errors|pluralize }} below.
</p>
{% endif %}
<form action="/uregister/" method="post">
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit">
</form>
这是 uregister 的视图:
def uregister(request):
if request.method == 'POST':
form = PersonalInfo(request.POST)
if form.is_valid():
cd = form.cleaned_data
per_job = Personal(cd['Name'], cd['Email_ID'], cd['Address'], cd['Contact_Phone'], cd['Image'])
per_job.save()
return HttpResponseRedirect('/')
else:
form = PersonalInfo()
return render_to_response('register.html', {'form': form}, context_instance=RequestContext(request))
This是 models.py 中的个人模型:
class Personal(models.Model):
name = models.CharField(max_length=20)
email = models.EmailField(blank=True,null=True)
address = models.CharField(max_length=50,blank=True,null=True)
contact = models.CharField(max_length=20)
pic = models.FileField(upload_to='image/',blank=True,null=True)
我得到的错误是:
invalid literal for int() with base 10: 'check'
并且
Exception Type: ValueError
Exception Value: invalid literal for int() with base 10: 'check'
Exception Location: /usr/local/lib/python2.6/dist-packages/django/db/models/fields/__init__.py in get_prep_value, line 479
Check
是我在虚拟数据中给出的名称。 谁能告诉我哪里错了?请。
更新: 痕迹
Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/nagaraj/ghar/gharnivas/views.py" in uregister
49. per_job.save()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py" in save
460. self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py" in save_base
522. manager.using(using).filter(pk=pk_val).exists())):
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py" in filter
550. return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py" in _filter_or_exclude
568. clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py" in add_q
1172. can_reuse=used_aliases, force_having=force_having)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py" in add_filter
1107. connector)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/where.py" in add
67. value = obj.prepare(lookup_type, value)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/where.py" in prepare
316. return self.field.get_prep_lookup(lookup_type, value)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/__init__.py" in get_prep_lookup
292. return self.get_prep_value(value)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/__init__.py" in get_prep_value
479. return int(value)
Exception Type: ValueError at /uregister/
Exception Value: invalid literal for int() with base 10: 'check
I have used forms of Django in this manner and have got an error:
Error:invalid literal for int() with base 10: 'check
'
#this is forms.py
from django import forms
class PersonalInfo(forms.Form):
Name = forms.CharField(max_length=20)
Email_ID = forms.EmailField(required=False)
Address = forms.CharField(max_length=50,required=False)
Contact_Phone = forms.CharField(max_length=20)
Image = forms.FileField(required=False)
The PersonalInfo is used in register.html
#This is view.py, register calling register.html
def register(request):
form = PersonalInfo()
return render_to_response('register.html', {'form':form}, context_instance=RequestContext(request))
In register.html this is the way I will use it :
{% if form.errors %}
<p style="color: red;">
Please correct the error{{ form.errors|pluralize }} below.
</p>
{% endif %}
<form action="/uregister/" method="post">
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit">
</form>
This is views of uregister:
def uregister(request):
if request.method == 'POST':
form = PersonalInfo(request.POST)
if form.is_valid():
cd = form.cleaned_data
per_job = Personal(cd['Name'], cd['Email_ID'], cd['Address'], cd['Contact_Phone'], cd['Image'])
per_job.save()
return HttpResponseRedirect('/')
else:
form = PersonalInfo()
return render_to_response('register.html', {'form': form}, context_instance=RequestContext(request))
This is the Personal model in models.py:
class Personal(models.Model):
name = models.CharField(max_length=20)
email = models.EmailField(blank=True,null=True)
address = models.CharField(max_length=50,blank=True,null=True)
contact = models.CharField(max_length=20)
pic = models.FileField(upload_to='image/',blank=True,null=True)
The error I get is :
invalid literal for int() with base 10: 'check'
and
Exception Type: ValueError
Exception Value: invalid literal for int() with base 10: 'check'
Exception Location: /usr/local/lib/python2.6/dist-packages/django/db/models/fields/__init__.py in get_prep_value, line 479
Check
is the name I had given as in dummy data.
Can anyone tell me where i am going wrong? Please.
Update:
Trace
Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/nagaraj/ghar/gharnivas/views.py" in uregister
49. per_job.save()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py" in save
460. self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py" in save_base
522. manager.using(using).filter(pk=pk_val).exists())):
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py" in filter
550. return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py" in _filter_or_exclude
568. clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py" in add_q
1172. can_reuse=used_aliases, force_having=force_having)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py" in add_filter
1107. connector)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/where.py" in add
67. value = obj.prepare(lookup_type, value)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/where.py" in prepare
316. return self.field.get_prep_lookup(lookup_type, value)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/__init__.py" in get_prep_lookup
292. return self.get_prep_value(value)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/__init__.py" in get_prep_value
479. return int(value)
Exception Type: ValueError at /uregister/
Exception Value: invalid literal for int() with base 10: 'check
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于
我不知道是否可以创建仅具有位置参数的模型实例,但文档中没有提及。您应该使用关键字参数:
您的错误我们看到的结果可能是尝试将非整数值分配给具有对象 ID 的默认数据库字段,因此可能是由此引起的。
关于其他事情:
enctype="multipart/form-data"
这是正确处理上传文件所必需的。uregister
视图的else:
分支中的空实例。I think the problem is in line
I don't know if it's possible to create a model instance with only positional parameters, but it's not mentioned in the docs. You should be using keyword parameters:
The error you're seeing probably results from trying to assing non-integer value to the default database field with object ID, so it might be caused by this.
Regarding the other things:
enctype="multipart/form-data"
which is required for correctly processing uploaded files.else:
branch of youruregister
view.