Django中的视图错误

时间:2011-05-07 14:40:56

标签: python django django-forms django-views

我以这种方式使用了Django的形式并且出现了错误: 错误: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)

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

这是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

1 个答案:

答案 0 :(得分:2)

我认为问题在于

per_job = Personal(cd['Name'], cd['Email_ID'], cd['Address'], cd['Contact_Phone'], cd['Image'])

我不知道是否可以创建仅包含位置参数的模型实例,但在文档中没有提到。你应该是using keyword parameters

per_job = Personal(name=cd['Name'], email=cd['Email_ID'], etc.

您看到的错误可能是因为尝试使用对象ID将非整数值分配给默认数据库字段,因此可能是由此引起的。


关于其他事项:

  • 图片未被存储可能是因为您没有正确处理上传文件所需的using form attribute enctype="multipart/form-data"
  • 错误最有可能不会显示,因为它们在验证后包含在表单中,并且您将其替换为else:视图的uregister分支中的空实例。