Python Django:ValueError位于/save_page/-int()的文本无效,以10为基数:';我的世界';

Python Django:ValueError位于/save_page/-int()的文本无效,以10为基数:';我的世界';,python,django,wiki,Python,Django,Wiki,我只是试图将POST数组中的数据保存到数据库中,但出现了此错误。 以下是我的错误所在视图: def save_page(request): if request.method == 'POST': if "title" in request.POST and "content" in request.POST and request.POST["title"]: c = {} c.

我只是试图将POST数组中的数据保存到数据库中,但出现了此错误。 以下是我的错误所在视图:

    def save_page(request):
        if request.method == 'POST':

            if "title" in request.POST and "content" in request.POST and request.POST["title"]:

                c = {}
                c.update(csrf(request))
                try:

                    article = Article.objects.get(pk=request.POST["title"])
                    article.content = request.POST["content"]
                except Article.DoesNotExist:
                    #the error arises here
                    article = Article(request.POST["title"], request.POST["content"])
                    article.save()
                    return HttpResponseRedirect("Wikipedia/"+request.POST["title"]+"/")
                return render_to_response("create_page.html", c)
这是示范文章

    class Article(models.Model):
        title = models.CharField(max_length=20)
        content = models.TextField(blank=True)
在我的输入字段中,我只是简单地将“我的世界”作为标题。和“我的朋友”作为内容

完整的回溯:

Environment:


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

Django Version: 1.5.5
Python Version: 2.7.5
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'Wiki')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "C:/Users/Zaid/PycharmProjects/Wikipedia\Wiki\views.py" in save_page
  61.                 article = Article.objects.get(pk=request.POST["title"])
File "C:\Python27\lib\site-packages\django\db\models\manager.py" in get
  143.         return self.get_query_set().get(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in get
  395.         clone = self.filter(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in filter
  669.         return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
  687.             clone.query.add_q(Q(*args, **kwargs))
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in add_q
  1271.                             can_reuse=used_aliases, force_having=force_having)
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in add_filter
  1202.                 connector)
File "C:\Python27\lib\site-packages\django\db\models\sql\where.py" in add
  71.             value = obj.prepare(lookup_type, value)
File "C:\Python27\lib\site-packages\django\db\models\sql\where.py" in prepare
  339.             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
  322.             return self.get_prep_value(value)
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
  555.         return int(value)

Exception Type: ValueError at /save_page/
Exception Value: invalid literal for int() with base 10: 'My World'

由于您没有在模型中指定PK,因此您的文章的PK是Django默认值;一个自动递增的整数(可以通过“id”或“pk”引用)。因此,当您查询

article = Article.objects.get(pk=request.POST["title"])
您得到了上述错误,因为Django希望PK为整数。您可以:

  • primary\u key=True
    添加到
    title
    字段定义中(然后需要重新创建基础表,无需修改代码)
  • unique=True
    添加到
    title
    字段定义中,并使用
    get(title=request.POST[“title”])
    进行查询,必须在数据库级别创建唯一约束

当然,您可以按标题进行查询,而无需附加唯一约束,但如果两个模型上存在相同的标题,并且您通过.get()进行查找,则将来可能会导致错误

您需要检查模板中的URL。 您需要将整数id传递给url{{user.id},因为url在模板中需要有整数值。 例如url:-/polls/{{user.id}/
希望这对其他人有用。

你能给出完整的回溯吗?我明白你为什么不愿意把它放在第一位:p