简体   繁体   English

Django TypeError int()参数必须是字符串或数字,而不是'QueryDict'

[英]Django TypeError int() argument must be a string or a number, not 'QueryDict'

Getting a TypeError when I click submit on my index page 单击索引页面上的提交时出现TypeError

int() argument must be a string or a number, not 'QueryDict'

I want to pass in the user and message variables so that when they submit the message, both user and message get saved in the database. 我想传入用户和消息变量,以便在提交消息时,用户和消息都保存在数据库中。 At this moment, I don't see how in my index.html I am supposed to use those variables passed in (user and message) to relay back that information. 此时,我没有看到我的index.html中我应该如何使用传入的那些变量(用户和消息)来中继该信息。

Traceback 追溯

    Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/

Django Version: 1.6.1
Python Version: 2.7.2
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'SimpleMessage')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Josh\Documents\Message\SimpleMessage\SimpleMessage\views.py" in index
  56.       u.save()
File "C:\Python27\lib\site-packages\django\db\models\base.py" in save
  545.                        force_update=force_update, update_fields=update_fields)
File "C:\Python27\lib\site-packages\django\db\models\base.py" in save_base
  573.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Python27\lib\site-packages\django\db\models\base.py" in _save_table
  635.                                       forced_update)
File "C:\Python27\lib\site-packages\django\db\models\base.py" in _do_update
  665.         filtered = base_qs.filter(pk=pk_val)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in filter
  590.         return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
  608.             clone.query.add_q(Q(*args, **kwargs))
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in add_q
  1198.         clause = self._add_q(where_part, used_aliases)
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in _add_q
  1232.                     current_negated=current_negated)
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in build_filter
  1125.         clause.add(constraint, AND)
File "C:\Python27\lib\site-packages\django\utils\tree.py" in add
  104.         data = self._prepare_data(data)
File "C:\Python27\lib\site-packages\django\db\models\sql\where.py" in _prepare_data
  79.             value = obj.prepare(lookup_type, value)
File "C:\Python27\lib\site-packages\django\db\models\sql\where.py" in prepare
  352.             return self.field.get_prep_lookup(lookup_type, value)
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_lookup
  369.             return self.get_prep_value(value)
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
  613.         return int(value)

Exception Type: TypeError at /
Exception Value: int() argument must be a string or a number, not 'QueryDict'

Models 楷模

class User (models.Model):
    name = models.CharField(max_length=20)

    def __unicode__(self):              
        return self.name

class Message (models.Model):
    content = models.TextField(max_length=140)
    user = models.ForeignKey(User)
    time = models.DateTimeField()

    def __unicode__(self):              
        return self.content

views.py views.py

def index (request):
    if request.method == 'POST':
        u = User(request.POST)
        m = Message(request.POST)
        u.save()
        m.save()
        return render_to_response('index.html', {
                'user': u,
                'message': m,
                }, RequestContext(request))
    else:
        u = User()
        m = Message()
        return render_to_response('index.html', {
                'user': u,
                'message': m,
                }, RequestContext(request))

index.html 的index.html

<form action="{% url 'index' %}" method = "post">
{% csrf_token %}
<input type="text" name="user" id="user" maxlength="20" placeholder = "Username">
<br>
<br>
<textarea rows="4" cols="35" name="text" maxlength="140" placeholder = "Message goes here"></textarea><br>
<input type="submit" value="Submit">
</form>

You directly pass the POST query dict to instruction of Model. 您直接将POST查询dict传递给Model的指令。

Change it: 更改:

u = User(request.POST)

To: 至:

u = User(name=request.POST.get('user'))

And, this: 还有这个:

m = Message(request.POST)

To: 至:

m = Message(content=request.POST.get('text'), user=u)

Also if you want to adding time of message automatically, change this line: 此外,如果您想自动添加消息时间,请更改此行:

time = models.DateTimeField()

To: 至:

time = models.DateTimeField(auto_now_add=True)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django保存新记录,int()参数必须是字符串或数字,而不是&#39;QueryDict&#39; - Django Saving new record, int() argument must be a string or a number, not 'QueryDict' TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是“QueryDict” - TypeError: int() argument must be a string, a bytes-like object or a number, not 'QueryDict' Django int() 参数必须是字符串、类似字节的对象或数字,而不是“QueryDict” - Django int() argument must be a string, a bytes-like object or a number, not 'QueryDict' int() 参数必须是字符串、类似字节的对象或数字,而不是 &#39;QueryDict&#39; - int() argument must be a string, a bytes-like object or a number, not 'QueryDict' Django-TypeError:int()参数必须是字符串或数字,而不是&#39;dict&#39; - Django - TypeError: int() argument must be a string or a number, not 'dict' Django错误:TypeError:int()参数必须是字符串或数字,而不是“ BuildsTable” - Django Error: TypeError: int() argument must be a string or a number, not 'BuildsTable' TypeError:int()参数必须是字符串或数字,而不是&#39;Binary&#39; - TypeError: int() argument must be a string or a number, not 'Binary' TypeError:int()参数必须是字符串或数字,而不是“列表” - TypeError: int() argument must be a string or a number, not 'list' TypeError:int()参数必须是字符串或数字,而不是&#39;tuple&#39; - TypeError: int() argument must be a string or a number, not 'tuple' 类型错误:INT()参数必须是字符串或数字,而不是“字典” - TypeError: int() argument must be a string or a number, not 'dict'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM