小编Kiw*_*iwi的帖子

艰苦学习Python练习17额外问题(S)

我正在做Zed Shaw的奇妙学习Python The Hard Way,但是另外一个问题让我感到难过:第9-10行可以写成一行,怎么样?我尝试了一些不同的想法,但无济于事.我可以继续前进,但那有什么乐趣呢?

from sys import argv
from os.path import exists

script, from_file, to_file = argv

print "Copying from %s to %s" % (from_file, to_file)

# we could do these two on one line too, how?
input = open(from_file)
indata = input.read()

print "The input file is %d bytes long" % len(indata)

print "Does the output file exist? %r" % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()

output = open(to_file, 'w')
output.write(indata) …
Run Code Online (Sandbox Code Playgroud)

python python-2.x

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

没有连接,因为目标机器主动拒绝它(Django)

我已经关注了Django Book直到第七章,我目前正在搞乱表格,GET,POST和所有这些善良.有一次,指南让我在填写并发送表单后弄清楚反应,但是当我发送表单数据时,我收到此错误:

error at /contact/
[Errno 10061] No connection could be made because the target machine actively refused itRequest Method: POST
Request URL:    http://127.0.0.1:8000/contact/
Django Version: 1.3
Exception Type: error
Exception Value:    [Errno 10061] No connection could be made because the target machine actively refused it
Exception Location: C:\Python27\lib\socket.py in create_connection, line 571
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.2

换句话说,在此之前我没有任何端口问题.我只是在Windows防火墙中明确地打开了端口10061,但看起来却无济于事.(runserver在我改变规则后,我关闭并打开了.)

我正在运行Windows 7,我的问题的要点是这个错误消息究竟意味着什么比如何处理它更多(当然,两者都是可取的).

编辑:我还转发了Windows防火墙中的端口8000(适用于所有配置文件,TCP),但我仍然得到看起来像相同的错误.

python django

13
推荐指数
2
解决办法
4万
查看次数

我可以屏蔽Chrome的<textarea>拖放功能吗?

在Chrome中,<textarea>可以拉动字段的右下角以展开文本区域.这有时会有其优点,但有时却没有.(这个讨论是另一次.)

我想知道的是如果可能的话我可以阻止这种行为.我正在考虑一些JS/jQuery dingus的内容,但我真的不知道Google是如何编写该功能的.

谁有人涉足这个?

html jquery google-chrome jquery-plugins

12
推荐指数
2
解决办法
1万
查看次数

不能在Django中使用环境变量进行设置

在尝试查找存储和保存settings.py和数据库之外的设置的地方时,我使用了environment.json作为环境变量.我在settings.py中导入这些.

我的问题是,当我尝试在我的环境中更改或存储新值时env,settings.py不会注意到更改 - 可能是因为Django读取了settings.py的时间和次数.

有没有办法可以按照我想要的方式使用我的环境变量?

# settings.py
import json
with open('/home/dotcloud/environment.json') as f:
    env = json.load(f)
EMAIL_HOST = env.get('EMAIL_PORT', '500')

# views.py
import json
def site_configuration(request):
    with open('/home/dotcloud/environment.json') as f:
        env = json.load(f)
    if request.method == 'POST':
        os.environ['EMAIL_PORT'] = request.POST['email_port']
    return render(request, ...)

# python manage.py shell demo
>>> import json
>>> with open('/home/dotcloud/environment.json') as f:
...     env = json.load(f)
... 
>>> project_settings.EMAIL_PORT
'500'
>>> env['EMAIL_PORT']
Traceback (most recent call last):
  File "<console>", line 1, in …
Run Code Online (Sandbox Code Playgroud)

python django environment-variables

12
推荐指数
2
解决办法
1万
查看次数

尝试在Windows上安装Python bcrypt的许多问题(x64)

这个问题已在其他地方提到,但它没有提供可行的解决方案,所以我仍然希望打开一张票.

c:\Users\me\Desktop\temp\py-bcrypt-0.2>setup.py install build --compiler=min
gw32
running install
running build
running build_py
running build_ext
building 'bcrypt._bcrypt' extension
C:\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall -IC:\Python27\include -IC:\Python27\PC -c bcrypt/bcrypt_python.c -o build\temp.win32-2.7\Release\bcrypt\bcrypt_python.o
bcrypt/bcrypt_python.c:29:26: error: expected declaration specifiers or '...' before 'u_int8_t'
bcrypt/bcrypt_python.c:29:38: error: expected declaration specifiers or '...' before 'u_int16_t'
bcrypt/bcrypt_python.c:29:49: error: expected declaration specifiers or '...' before 'u_int8_t'
bcrypt/bcrypt_python.c: In function 'bcrypt_encode_salt':
bcrypt/bcrypt_python.c:56:2: error: too many arguments to function 'encode_salt'

bcrypt/bcrypt_python.c:29:6: note: declared here
error: command 'gcc' failed with exit …

python windows windows-7 windows64

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

IntegrityError:(1062,密钥重复输入)

我试图清除我的数据库,mydb并重新填充它以查看我的Django模型的一些更改的影响.然后这一切都发生了.我回滚到我早期的模型设置,但我仍然得到错误.

我不是MySQL专家,我不能完全弄清楚问题以及如何处理它; 我尝试使用其他名称创建一个新数据库,但这没有发生.我认为这是我的Django项目创建的问题.

这是所有信息:


C:\Users...>python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log
Creating table forum_category
Creating table forum_thread

Creating table forum_post

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes …

python mysql django

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

难以理解的Django Runserver错误

我在Django遇到了很多问题(并非所有问题都解决了),但这是迄今为止最奇怪,最难以理解的问题.当我加载页面时会发生这种情况:

>python manage.py runserver

Validating models...

0 errors found
Django version 1.3.1, using settings 'ponysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[01/Nov/2011 01:21:49] "GET / HTTP/1.0" 500 102747

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 284, in run
    self.finish_response()
  File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 324, in finish_response
    self.write(data)
  File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 420, in write
    self._write(data)
  File "C:\Python27\lib\socket.py", line 324, in write
    self.flush()
  File "C:\Python27\lib\socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10054] An existing connection was …

python django

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