Django 中的视图错误

发布于 2024-11-05 08:21:16 字数 4567 浏览 0 评论 0原文

我以这种方式使用了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦慕雪 2024-11-12 08:21:16

我认为问题在于

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

我不知道是否可以创建仅具有位置参数的模型实例,但文档中没有提及。您应该使用关键字参数

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

您的错误我们看到的结果可能是尝试将非整数值分配给具有对象 ID 的默认数据库字段,因此可能是由此引起的。


关于其他事情:

  • 图像未存储可能是因为您不是 使用表单属性 enctype="multipart/form-data" 这是正确处理上传文件所必需的。
  • 错误未显示很可能是因为它们在验证后包含在表单中,并且您将其替换为 uregister 视图的 else: 分支中的空实例。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文