我知道这里有很多帖子都有相同的标题和错误。我经历了尽可能多的事情,但发布的代码与我的相似。因此我不得不问。
我正在尝试将“游戏”对象保存到数据库中。相关的源文件发布在下面:
models.py
from django.contrib.auth.models import User
from django.db import models
class Usertypes(models.Model):
user = models.OneToOneField(User)
usertype = models.TextField()
def __unicode__(self):
return self.user_name
class Games(models.Model):
name = models.CharField(max_length=100,unique=True)
category = models.CharField(max_length=100)
url = models.URLField()
developer = models.OneToOneField(User)
price = models.FloatField()
def __unicode__(self):
return self.name
views.py:
def addgame(request):
c={}
c.update(csrf(request))
saved = False
if request.method == 'POST':
name = request.POST['name']
#print(name)
category = request.POST['category']
#print(category)
url = request.POST['url']
#print(url)
developer = request.POST['developer']
#print(developer)
price = request.POST['price']
#print(price)
game = Games(name,category,url,developer,price)
game.save()
saved = True
return render_to_response('gamestore/developer_homepage.html',{'saved':saved},context_instance=RequestContext(request))
urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
import gamestore.views
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'wsdProject.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$','gamestore.views.home'),
url(r'^login/','gamestore.views.login_view'),
url(r'^logout/','gamestore.views.logout_view'),
url(r'^registration/','gamestore.views.registration'),
url(r'^addgame/','gamestore.views.addgame'),
url(r'^gamestore/', include('gamestore.urls', namespace="gamestore")),
最后,我的错误:
/ addgame /的ValueError int()的基数为10的无效文字:'NFS'
错误追溯:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/addgame/
Django Version: 1.7.1
Python Version: 3.2.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'gamestore')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/home/mukhera3/djangoenv/lib/python3.2/site-packages/django/core/handlers/base.py" in get_response 111.response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/mukhera3/Desktop/game_store/wsdProject/gamestore/views.py" in addgame 137.game.save()
File "/home/mukhera3/djangoenv/lib/python3.2/site-packages/django/db/models/base.py" in save 591.force_update=force_update,update_fields=update_fields)
File "/home/mukhera3/djangoenv/lib/python3.2/site-packages/django/db/models/base.py" in save_base 619.updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/mukhera3/djangoenv/lib/python3.2/site-packages/django/db/models/base.py" in _save_table 681.forced_update)
File "/home/mukhera3/djangoenv/lib/python3.2/site-packages/django/db/models/base.py" in _do_update 711.filtered = base_qs.filter(pk=pk_val)
File "/home/mukhera3/djangoenv/lib/python3.2/site-packages/django/db/models/query.py" in filter 691.return self._filter_or_exclude(False, *args, **kwargs)
File "/home/mukhera3/djangoenv/lib/python3.2/site-packages/django/db models/query.py" in _filter_or_exclude 709.clone.query.add_q(Q(*args, **kwargs))
File "/home/mukhera3/djangoenv/lib/python3.2/site-packages/django/db/models/sql/query.py" in add_q 1287. clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/home/mukhera3/djangoenv/lib/python3.2/site-packages/django/db/models/sql/query.py" in _add_q 1314. current_negated=current_negated, connector=connector)
File "/home/mukhera3/djangoenv/lib/python3.2/site-packages/django/db/models/sql/query.py" in build_filter 1186.condition = self.build_lookup(lookups, col, value)
File "/home/mukhera3/djangoenv/lib/python3.2/site-packages/django/db/models/sql/query.py" in build_lookup 1094. return final_lookup(lhs, rhs)
File "/home/mukhera3/djangoenv/lib/python3.2/site-packages/django/db/models/lookups.py" in __init__82.self.rhs = self.get_prep_lookup()
File "/home/mukhera3/djangoenv/lib/python3.2/site-packages/django/db/models/lookups.py" in get_prep_lookup 85.return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "/home/mukhera3/djangoenv/lib/python3.2/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup 646.return self.get_prep_value(value)
File "/home/mukhera3/djangoenv/lib/python3.2/site-packages/django/db/models/fields/__init__.py" in get_prep_value 915.return int(value)
Exception Type: ValueError at /addgame/
Exception Value: invalid literal for int() with base 10: 'NFS'
有人可以建议我应该如何更改Games对象的保存方式?在此先感谢!!
答案 0 :(得分:2)
你真的应该使用ModelForm,这将使一切变得更加容易。
也就是说,您可以通过获取开发人员的实际User对象来修复代码:
developer = User.objects.get(username=request.POST['developer'])
但如果你使用了ModelForm,则不需要这样做。此外,您将拥有实际验证用户条目的优势,而不是在数据丢失时崩溃,而现在将会发生这种情况。