Django fixtures:通过JSON将HTML数据导入TextField

时间:2014-02-03 07:51:15

标签: python html json django fixtures

我正在从另一个内容管理系统将我的网站切换到Django,我无法使用fixtures将HTML导入Django,特别是“Events”,这是我的Django应用程序中的模型。该模型如下:

class Event(models.Model):
    event_name = models.CharField(max_length=100)
    event_date = models.DateTimeField()
    event_city = models.CharField(max_length=100)
    event_province = models.CharField(max_length=100)
    event_location = models.CharField(max_length=100, blank=True)
    event_details = models.TextField(blank=True)

event_details是HTML,包含指定事件的详细信息。使用syncdb导入数据时,我收到 DeserializationError:问题安装夹具'events.json':无效的控制字符位于:第274行第73列(字符7090)

以下是我要导入的json示例:

{
  "model": "events.Event",
  "pk": 26,
  "fields": {
  "event_name": "Random Event",
  "event_date": "2008-09-06 00:00:00+00:00",
  "event_city": "Toronto",
  "event_province": "ON",
  "additional_info": { "data": "Promoter: Random Person Productions<BR>
Contact: John Doe: (555) 555-7777<BR>
Promoter Website: <A HREF="http://www.foo.com">www.foo.com</A>" }
  }
},

错误发生在“additional_info”第一行的第一个BR之后。我做错了什么?

问候。

1 个答案:

答案 0 :(得分:1)

可能有两件事:

  1. <A>标记中的双引号。双引号是JSON中的字符串分隔符,因此当它们包含在JSON字符串they should be escaped with a backslash中时,如下所示:

    { "data": "Promoter: Random Person Productions<BR>
    Contact: John Doe: (555) 555-7777<BR>
    Promoter Website: <A HREF=\"http://www.foo.com\">www.foo.com</A>" }
    
  2. HTML中的换行符。 JSON strings also can’t contain newline characters,所以如果它们实际上是你的JSON(而不是你在Stack Overflow上输入代码时为了可读性而放入的东西)并且你希望它们留在那里,你也需要逃避它们:

    { "data": "Promoter: Random Person Productions<BR>\\nContact: John Doe: (555) 555-7777<BR>\\nPromoter Website: <A HREF=\"http://www.foo.com\">www.foo.com</A>" }
    
相关问题