我a.py
在我的mysite文件中构建了一个,
a.py:
from django.core.management import setup_environ
from mysite import settings
setup_environ(settings)
from django.contrib.sites.models import Site
domain = Site.objects.get_current().domain
print domain
Run Code Online (Sandbox Code Playgroud)
它打印:
:example.com
如何将"域"更改为127.0.0.1:8000?
from django.core.management import setup_environ
from register import settings
setup_environ(settings)
from django.contrib.sites.models import Site
#domain = Site.objects.get_current().domain
#print domain
from django.contrib.auth.models import User
obj=Site.objects.get(id=1).update(name='sss')
print obj
Run Code Online (Sandbox Code Playgroud)
错误:
Traceback (most recent call last):
File "D:\zjm_code\register\a.py", line 13, in <module>
obj=Site.objects.get(id=1).update(name='sss')
AttributeError: 'Site' object has no attribute 'update'
Run Code Online (Sandbox Code Playgroud) 这是mootools代码:
var myString = "{subject} is {property_1} and {property_2}.";
var myObject = {subject: 'Jack Bauer', property_1: 'our lord', property_2: 'savior'};
myString.substitute(myObject);
Run Code Online (Sandbox Code Playgroud)
并且jquery有这个方法吗?或者喜欢这种方法?
我可以这样读取a.txt
文件:
text = open('a.txt', 'rb').read()
Run Code Online (Sandbox Code Playgroud)
但当我把a.txt
到media
文件夹中,这不起作用:
text = open('/media/a.txt', 'rb').read()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
IOError at /
[Errno 13] file not accessible: '/media/a.txt'
Run Code Online (Sandbox Code Playgroud)
如何从media
目录中读取文件?
我的代码是:
print os.urandom(64)
Run Code Online (Sandbox Code Playgroud)
哪个输出:
> "D:\Python25\pythonw.exe" "D:\zjm_code\a.py"
\xd0\xc8=<\xdbD'
\xdf\xf0\xb3>\xfc\xf2\x99\x93
=S\xb2\xcd'\xdbD\x8d\xd0\\xbc{&YkD[\xdd\x8b\xbd\x82\x9e\xad\xd5\x90\x90\xdcD9\xbf9.\xeb\x9b>\xef#n\x84
Run Code Online (Sandbox Code Playgroud)
这是不可读的,所以我尝试了这个:
print os.urandom(64).decode("utf-8")
Run Code Online (Sandbox Code Playgroud)
但后来我得到:
> "D:\Python25\pythonw.exe" "D:\zjm_code\a.py"
Traceback (most recent call last):
File "D:\zjm_code\a.py", line 17, in <module>
print os.urandom(64).decode("utf-8")
File "D:\Python25\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-3: invalid data
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能获得人类可读的输出?
WTForms是用于python web开发的表单验证和呈现库
我写这个代码来检查两个密码是否相同:
from wtforms import Form, BooleanField, TextField, validators
class SignUpForm(Form):
username = TextField('Username', [validators.Length(min=4, max=25)])
email = TextField('Email', [validators.Length(min=6, max=120), validators.Email()])
password1 = PasswordField('Password1')
password2 = PasswordField('Password2')
def sameP(self):
return self.password1 ==self.password2
Run Code Online (Sandbox Code Playgroud)
但是,我想知道:WTForms可以自己做.
谢谢
class a:
def __init__(self):
self._b()#why here use _b,not b,What's the difference
self._c='cccc'#why here use _c,not c,What's the difference
def _b():
print 'bbbb'
Run Code Online (Sandbox Code Playgroud)
a.py
class a:
def __init__(self):
self._b()#why here use _b,not b,What's the difference
self._c='cccc'#why here use _c,not c,What's the difference
def _b(self):
print 'bbbb'
Run Code Online (Sandbox Code Playgroud)
b.py
from a import *
b=a()
b._b()
print b._c
Run Code Online (Sandbox Code Playgroud)
它打印bbbb bbbb bbbb bbbb cccc
为什么可以打印出这些,不是_b和_c私有变量.
此代码位于django.utils.functional.py中
class __proxy__(Promise):
Run Code Online (Sandbox Code Playgroud)
谢谢
对于代码:
def a(x):
if x=='s':
__import__('os') #I think __import__ == import
print os.path
Run Code Online (Sandbox Code Playgroud)
为什么不print a('os')
打印os.path?
我的下一个问题是:为什么以下代码使用__import__('some')
而不是像a = __import__('os')
?
def import_module(name, package=None):
if name.startswith('.'):
if not package:
raise TypeError("relative imports require the 'package' argument")
level = 0
for character in name:
if character != '.':
break
level += 1
name = _resolve_name(name[level:], package, level)
__import__(name) #Why does it do this
return sys.modules[name] #Instead of `return __import__(name)`
Run Code Online (Sandbox Code Playgroud) class a(object):
b = 'bbbb'
def __init__(self):
self.c = 'cccc'
Run Code Online (Sandbox Code Playgroud)
我认为他们是一样的; 有什么不同吗?