单击索引页面上的提交时出现TypeError
int() argument must be a string or a number, not 'QueryDict'
我想传递用户和消息变量,以便在提交消息时,用户和消息都会保存在数据库中。在这一刻,我不知道在我的index.html中我应该如何使用传入的那些变量(用户和消息)来传回这些信息。
回溯
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'
模型
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
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
<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>
答案 0 :(得分:6)
您直接将POST
查询字典传递给Model的指令。
改变它:
u = User(request.POST)
要:
u = User(name=request.POST.get('user'))
而且,这个:
m = Message(request.POST)
要:
m = Message(content=request.POST.get('text'), user=u)
此外,如果您想自动添加消息时间,请更改此行:
time = models.DateTimeField()
要:
time = models.DateTimeField(auto_now_add=True)