小编Irm*_*nis的帖子

您试图在没有默认值的情况下向userprofile添加不可为空的字段"new_field"

我知道从Django 1.7我不需要使用South或任何其他迁移系统,所以我只是使用简单的命令 python manage.py makemigrations

但是,我得到的只是这个错误:

You are trying to add a non-nullable field 'new_field' to userprofile without a default;
we can't do that (the database needs something to populate existing rows).
Run Code Online (Sandbox Code Playgroud)

这是models.py:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    website = models.URLField(blank=True)
    new_field = models.CharField(max_length=140)
Run Code Online (Sandbox Code Playgroud)

有什么选择?

python django

85
推荐指数
7
解决办法
12万
查看次数

BeautifulSoup更改HTML

我注意到,当我从网上获取带有Beautiful Soup的HTML时,它会以某种方式发生变化.这是我用来获取它的代码:

from bs4 import BeautifulSoup
import requests
url ="http://www.basketnews.lt/lygos/59-nacionaline-krepsinio-asociacija/2013/naujienos.html"
r = requests.get(url)
soup = BeautifulSoup(r.text)
print soup
Run Code Online (Sandbox Code Playgroud)

这是原始HTML的一部分:

<a href="/news-73149-valanciunui-ir-raptors-sezonas-baigtas-foto-statistika.html">Valan?i?nui ir Raptors sezonas baigtas <span class="title_description">(foto, statistika)</span></a>`
Run Code Online (Sandbox Code Playgroud)

以下是与Beautiful Soup一起使用的HTML的相同部分:

<a href="/news-73149-valanciunui-ir-raptors-sezonas-baigtas-foto-statistika.html">ValanÄiÅ«nui ir âRaptorsâ sezonas baigtas <span class="title_description">(foto, statistika)</span></a>
Run Code Online (Sandbox Code Playgroud)

您将看到我正在解析的HTML中的文本是如何混乱的.问题出在哪儿?

python beautifulsoup python-requests

3
推荐指数
1
解决办法
1395
查看次数

如何打印奇怪的字符串字符?

我有一个名字的清单.其中一些由像★或™这样的奇怪字符组成.当我迭代扔列表时,打印就好了:

? StatTrak™ Huntsman Knife | Safari Mesh (Battle-Scarred)
Souvenir USP-S | Night Ops (Well-Worn)
StatTrak™ G3SG1 | The Executioner (Minimal Wear)
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试逐个打印时:

print a[0]
'\xe2\x98\x85 StatTrak\xe2\x84\xa2 Huntsman Knife | Safari Mesh (Battle-Scarred)'
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题呢?

更新:

迭代:

list = ['? StatTrak™ Huntsman Knife | Safari Mesh (Battle-Scarred)',
'Souvenir USP-S | Night Ops (Well-Worn)',
'StatTrak™ G3SG1 | The Executioner (Minimal Wear)']

for name in list:
    print name

>>> 
? StatTrak™ Huntsman Knife | Safari Mesh (Battle-Scarred)
Souvenir USP-S | Night Ops (Well-Worn) …
Run Code Online (Sandbox Code Playgroud)

python

3
推荐指数
1
解决办法
662
查看次数

Python函数返回无.为什么?

count = []

def problem14(n):
    count.append(n)
    if n == 1:
        return count
    if n % 2 == 0:
        n = n/2
        problem14(n)
    else:
        n = 3*n + 1
        problem14(n)


print problem14(13)
Run Code Online (Sandbox Code Playgroud)

所以这是我写的代码.我不知道为什么它会返回None,而在我看来它应该返回列表'count'.有帮助吗?

python python-2.7

1
推荐指数
1
解决办法
166
查看次数

findALL无法正常工作

这就是我试图获取所有链接的方式:

soup.find("div", attrs={"class": "vl-article-title"}).find("h3").find("span").find("a")
Run Code Online (Sandbox Code Playgroud)

这只找到第一个,但正如我所说,我需要所有这些.

为什么这不起作用:

soup.findAll("div", attrs={"class": "vl-article-title"}).find("h3").find("span").find("a")
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

'ResultSet' object has no attribute 'find'
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup

0
推荐指数
1
解决办法
158
查看次数

如何检查manytomany字段中的对象值?

如何检查值是否在很多领域?我想做这样的事情:

if value in object1.followers:
    #BLA BLA BLA
Run Code Online (Sandbox Code Playgroud)

但是'ManyRelatedManager' is not iterable.那么这样做的正确方法是什么?

python django

0
推荐指数
1
解决办法
952
查看次数