小编use*_*619的帖子

Python将日期字符串转换为datetime

我试图将日期字符串转换为Python但得到错误 -

串: '1986-09-22T00:00:00'

dob_python = datetime.strptime('1986-09-22T00:00:00' , '%Y-%m-%d%Z%H-%M-%S').date()
Run Code Online (Sandbox Code Playgroud)

错误:-

ValueError: time data '1986-09-22T00:00:00' does not match format '%Y-%m-%d%Z%H-%M-%S'
Run Code Online (Sandbox Code Playgroud)

python datetime

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

MultiPartParserError: - 边界无效

我试图使用Python请求模块将一些数据和文件发送到我的django rest应用程序但是得到以下错误.

    raise MultiPartParserError('Invalid boundary in multipart: %s' % boundary)
MultiPartParserError: Invalid boundary in multipart: None
Run Code Online (Sandbox Code Playgroud)

码:-

import requests
payload={'admins':[
                    {'first_name':'john'
                    ,'last_name':'white'
                    ,'job_title':'CEO'
                    ,'email':'test1@gmail.com'
                    },
                    {'first_name':'lisa'
                    ,'last_name':'markel'
                    ,'job_title':'CEO'
                    ,'email':'test2@gmail.com'
                    }
                    ],
        'company-detail':{'description':'We are a renowned engineering company'
                    ,'size':'1-10'
                    ,'industry':'Engineering'
                    ,'url':'http://try.com'
                    ,'logo':''
                    ,'addr1':'1280 wick ter'
                    ,'addr2':'1600'
                    ,'city':'rkville'
                    ,'state':'md'
                    ,'zip_cd':'12000'
                    ,'phone_number_1':'408-393-254'
                    ,'phone_number_2':'408-393-221'
                    ,'company_name':'GOOGLE'}
        }
files = {'upload_file':open('./test.py','rb')}
import json
headers = {'content-type' : 'application/json'}      
headers = {'content-type' : 'multipart/form-data'}      

#r = requests.post('http://127.0.0.1:8080/api/create-company-profile/',data=json.dumps(payload),headers=headers,files=files)
r = requests.post('http://127.0.0.1:8080/api/create-company-profile/',data=payload,headers=headers,files=files)
print r.status_code
print r.text
Run Code Online (Sandbox Code Playgroud)

Django代码: -

class …
Run Code Online (Sandbox Code Playgroud)

python django python-requests django-rest-framework

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

基于Django类的视图 - 线程安全

我对Java中的Web应用程序有一些经验.在servlet中,您必须将基于类的属性声明为final,因为它不是线程安全的.

当我尝试与基于Django类的视图进行比较时.假设下面的代码是线程安全的是否安全?我相信它的线程安全,但有些人让我知道我需要在Django中处理多线程应用程序的事项列表.

class MyFormView(View):
    form_class = MyForm
    initial = {'key': 'value'}
    template_name = 'form_template.html'

    def get(self, request, *args, **kwargs):
        form = self.form_class(initial=self.initial)
        return render(request, self.template_name, {'form': form})

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        person = Person()
        if form.is_valid():
            # <process form cleaned data>
            return HttpResponseRedirect('/success/')

        return render(request, self.template_name, {'form': form})
Run Code Online (Sandbox Code Playgroud)

编辑-1

创建了一个新类,并在POST方法中启动

Class Person(object):name ='sample'

django multithreading

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

Django F 字段迭代

我创建了一个简单的 Django 模型来探索 F 字段,但可以遍历该字段。

class PostgreSQLModel(models.Model):
    class Meta:
        abstract = True
        required_db_vendor = 'postgresql'

class NullableIntegerArrayModel(PostgreSQLModel):
    field = ArrayField(models.IntegerField(), blank=True, null=True)    
Run Code Online (Sandbox Code Playgroud)

现在,从我的 django shell 中,我创建了一个 F 对象,如下所示。不确定这个对象包含什么。它是否包含所有 ID?如何迭代结果?

>>> a=F('id')
>>> a
F(id)
>>> dir(a)
['ADD', 'BITAND', 'BITOR', 'DIV', 'MOD', 'MUL', 'POW', 'SUB', '__add__', '__and__', '__class__', '__delattr__', '__dict__', '__dir__', '__div__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__mod__', '__module__', '__mul__', '__ne__', '__new__', '__or__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rsub__', '__rtruediv__', …
Run Code Online (Sandbox Code Playgroud)

python django

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

Twilio - 生成6位数的随机数

我计划在我的Django应用程序中集成twilio进行双因素身份验证.

现在,我已经安装了twilio python模块并向我的号码发送了一些随机消息.

下一步是我发送一些随机的6位数字,这些数字是在银行应用程序或谷歌双因素身份验证中完成的.

如何在我的DJango应用程序中生成这些数字?

python django twilio

2
推荐指数
2
解决办法
2578
查看次数

更改 docker alpine 中的目录失败

尝试使用 alpine 映像构建 dockerfile 并安装一组目录。这是下面的脚本。在 mkdir 之前它工作正常,但不会更改为 /opt 来下载 git 代码。

git 代码仅下载到 /src。不确定为什么 cd /opt 命令不起作用。

FROM alpine
ADD . /src
WORKDIR /src
RUN apk update
RUN apk add git
RUN mkdir /opt
RUN cd /opt  && git clone --recursive https://github.com/Azure/azure-iot-sdk-python.git 
RUN ls -al 
RUN cd azure-iot-sdk-python && ls -al build_all/linux
Run Code Online (Sandbox Code Playgroud)

docker dockerfile docker-compose alpine-linux

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

在步骤之间找不到这种DSL方法“ publishHTML”

我有一个jenkins DSL步骤,该步骤运行我的python鼻子测试并创建一个单元测试覆盖率报告。

这是我的詹金斯舞台。

stage ('Unit Tests') {
            steps {
                sh """
                    #. venv/bin/activate
                    export PATH=${VIRTUAL_ENV}/bin:${PATH}
                    make unittest || true
                """
            }

            post {
                always {
                    junit keepLongStdio: true, testResults: 'report/nosetests.xml'
                    publishHTML target: [
                        reportDir: 'report/coverage',
                        reportFiles: 'index.html',
                        reportName: 'Coverage Report - Unit Test'
                    ]
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

我收到此错误-

java.lang.NoSuchMethodError:在步骤之间找不到这样的DSL方法“ publishHTML”。

如何解决此错误?我从不同的存储库获得了这段代码。

jenkins jenkins-plugins jenkins-pipeline

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

Python基础课程

我已经定义了一个类来处理文件但是当我尝试实例化类并传递文件名时会收到以下错误.让我知道会出现什么问题?

>>> class fileprocess:
...    def pread(self,filename):
...        print filename
...        f = open(filename,'w')
...        print f
>>> x = fileprocess
>>> x.pread('c:/test.txt')
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unbound method pread() must be called with
fileprocess instance as first argument (got nothing instead)
Run Code Online (Sandbox Code Playgroud)

python class

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

Python类定义 - import语句

我已经定义了2个类 - Person&Manager.Manager继承Person类.尝试导入Person类时出错.

代码如下.

Person.py

class Person:
    def __init__(self, name, age, pay=0, job=None):
        self.name = name
        self.age  = age
        self.pay  = pay
        self.job  = job

    def lastname(self):
        return  self.name.split()[-1]

    def giveraise(self,percent):
        #return self.pay *= (1.0 + percent)
        self.pay *= (1.0 + percent)
        return self.pay
Run Code Online (Sandbox Code Playgroud)

Manager.py

from Basics import Person

class Manager(Person):
    def giveRaise(self, percent, bonus=0.1):
        self.pay *= (1.0 + percent + bonus)        
        return self.pay
Run Code Online (Sandbox Code Playgroud)

错误陈述:

C:\ Python27 \基础> Person.py

C:\ Python27\Basics> Manager.py Traceback(最近一次调用最后一次):文件"C:\ Python27\Basics\Manager.py",第1行,来自Basics import Person ImportError:没有名为Basics的模块

为什么我发现No module found错误?

python

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

安装 reindent python

我对 Notepad++ 的 Python 缩进有很多问题。为了解决,我尝试安装 Python reindent 模块,但我不知道如何使用它。如果有人成功了,请告诉我..

这是我尝试的步骤。

1.使用简易安装我尝试安装包,

C:\Python27\Scripts>easy_install reindent
Searching for reindent
Reading http://pypi.python.org/simple/reindent/
Best match: Reindent 0.1.1
Downloading http://pypi.python.org/packages/source/R/Reindent/Reindent-0.1.1.tar
.gz#md5=878352c36c421a0b944607efba3b01ad
Processing Reindent-0.1.1.tar.gz
Running Reindent-0.1.1\setup.py -q bdist_egg --dist-dir c:\users\premvi~1\appdat
a\local\temp\easy_install-qdahih\Reindent-0.1.1\egg-dist-tmp-1z1zw8
zip_safe flag not set; analyzing archive contents...
Adding reindent 0.1.1 to easy-install.pth file
Installing reindent script to C:\Python27\Scripts

Installed c:\python27\lib\site-packages\reindent-0.1.1-py2.7.egg
Processing dependencies for reindent
Finished processing dependencies for reindent
Run Code Online (Sandbox Code Playgroud)
  1. 当我在 python GUI 上执行导入命令时,它成功了。

  2. 当我尝试使用它时出现以下错误。

>>> import reindent
>>> reindent -d c:/python27/wxpython/ch2-updateui.py
SyntaxError: invalid syntax
>>> reindent -d …
Run Code Online (Sandbox Code Playgroud)

python

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