开发者

Views error in Django

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

Che开发者_JAVA技巧ck 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


I think the problem is in line

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

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:

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

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:

  • Image is not stored probably because you're not using form attribute enctype="multipart/form-data" which is required for correctly processing uploaded files.
  • The errors are not displayed most probably because they're contained in the form after validation, and you're replacing that with an empty instance in else: branch of your uregister view.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜