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

Getting a TypeError when I click submit on my index page

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.

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

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>

Answers


You directly pass the POST query dict to instruction of 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)

Need Your Help

Where can I bind column cell view controls?

objective-c cocoa

I've been Googling for 3 hours, I can't find where to bind these things for Core Data in this textbook I'm reading. I just want to know where to find out where to bind it. I assume it's in the bind...

Unable to cast NSMutableDictionnary as [String: AnyObject]

swift2

I am moving to Swift 2.1 some code that was managing persistency CoreData. Though I get a strange error telling me that I can't cast NSMutableDictionnary to [String: AnyObject] which is something I...