Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django“预期字符串或缓冲区”错误_Python_Django - Fatal编程技术网

Python Django“预期字符串或缓冲区”错误

Python Django“预期字符串或缓冲区”错误,python,django,Python,Django,我正在写我的博客,但当我迁移它时,它给了我一个错误:预期的字符串或缓冲区错误。在第22行。它是一个收集视图时间的完整模型,我想它可能更改了view.py中的值,在创建模型后修改模型可能会有困难。 因此,我尝试更改默认值并在view_time中添加其他参数,但这不起作用。有没有人能帮我解决这个问题,或者给我一个明智的想法来重新设计一个更好的数据库?这是我的密码 models.py,文章包含查看时间 作为view.py的一部分,当访问其中一篇文章时,查看时间将添加为1 def blog_conten

我正在写我的博客,但当我迁移它时,它给了我一个错误:预期的字符串或缓冲区错误。在第22行。它是一个收集视图时间的完整模型,我想它可能更改了view.py中的值,在创建模型后修改模型可能会有困难。 因此,我尝试更改默认值并在view_time中添加其他参数,但这不起作用。有没有人能帮我解决这个问题,或者给我一个明智的想法来重新设计一个更好的数据库?这是我的密码

models.py,文章包含查看时间

作为view.py的一部分,当访问其中一篇文章时,查看时间将添加为1

def blog_content(request, page_num, error_form=None):
    context = {}
    form = CommentForm
    view_update = Article.objects.get(id=page_num)
    view_update.view_time += 1
    view_update.save()
    context['detail'] = view_update
    comments = Comment.objects.filter(belong_to=view_update)
    context['comments'] = comments
    print(comments.values().count())
    if error_form is not None:
        context['form'] = error_form
    else:
        context['form'] = form
    return render(request, 'blog_content.html', context)
随后出现错误

Applying blogapp.0018_comment_date...Traceback (most recent call last):##new added
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py", line 305, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py", line 356, in execute
    output = self.handle(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 202, in handle
    targets, plan, fake=fake, fake_initial=fake_initial
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py", line 97, in migrate
    state = self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py", line 132, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py", line 237, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 84, in database_forwards
    field,
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/sqlite3/schema.py", line 231, in add_field
    self._remake_table(model, create_fields=[field])
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/sqlite3/schema.py", line 113, in _remake_table
    self.effective_default(field)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 221, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 755, in get_db_prep_save
    prepared=False)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1280, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1275, in get_prep_value
    return self.to_python(value)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1237, in to_python
    parsed = parse_date(value)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/utils/dateparse.py", line 60, in parse_date
    match = date_re.match(value)
TypeError: expected string or buffer
选项应该是元组的一个iterable。你有一个三层的结构

试试这个:

class Article(models.Model):
    title = models.CharField(max_length=20)
    content = MarkdownField()
    date = models.DateField()
    description = models.TextField(blank=True)
    article_type = (
        ('Learning-Python', 'python'),
        ('Learning-Java', 'java'),
        ('Life', 'life'),
        ('Other', 'other')
    )
    type = models.CharField(max_length=20, choices=article_type, blank=True)
    lock = models.BooleanField(default=False)
    view_time = models.IntegerField(default=1)
如果要将类别添加到文章类型,则需要将其作为属性添加


希望这能有所帮助。

迁移中发生的错误与视图无关。因此,我应该如何修复它?请显示失败的迁移。什么意思,我应该如何显示迁移失败?很抱歉,真正的问题在于manage.py而不是models.py,我遗漏了一条重要消息,正在应用blogapp.0018_comment_date…回溯最近的通话last:,但我不知道它是如何妨碍的,因为这是我做的旧迁移。我在问题中转载了它,希望有人给出一个精辟的解决方案。这是不正确的。这本书展示了如何将选择分组到命名的组中。这是真的,但真正的问题是视图\时间=模型。IntegerFielddefault=1,如果您知道如何解决它,我将不胜感激。
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2017-07-26 12:19
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('blogapp', '0017_comment'),
    ]

    operations = [
        migrations.AddField(
            model_name='comment',
            name='date',
            field=models.DateField(default=True),
        ),
    ]
class Article(models.Model):
    title = models.CharField(max_length=20)
    content = MarkdownField()
    date = models.DateField()
    description = models.TextField(blank=True)
    article_type = (
        ('Learning-Python', 'python'),
        ('Learning-Java', 'java'),
        ('Life', 'life'),
        ('Other', 'other')
    )
    type = models.CharField(max_length=20, choices=article_type, blank=True)
    lock = models.BooleanField(default=False)
    view_time = models.IntegerField(default=1)